xref: /openbmc/linux/drivers/platform/x86/fujitsu-laptop.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*-*-linux-c-*-*/
3 
4 /*
5   Copyright (C) 2007,2008 Jonathan Woithe <jwoithe@just42.net>
6   Copyright (C) 2008 Peter Gruber <nokos@gmx.net>
7   Copyright (C) 2008 Tony Vroon <tony@linx.net>
8   Based on earlier work:
9     Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
10     Adrian Yee <brewt-fujitsu@brewt.org>
11 
12   Templated from msi-laptop.c and thinkpad_acpi.c which is copyright
13   by its respective authors.
14 
15  */
16 
17 /*
18  * fujitsu-laptop.c - Fujitsu laptop support, providing access to additional
19  * features made available on a range of Fujitsu laptops including the
20  * P2xxx/P5xxx/S2xxx/S6xxx/S7xxx series.
21  *
22  * This driver implements a vendor-specific backlight control interface for
23  * Fujitsu laptops and provides support for hotkeys present on certain Fujitsu
24  * laptops.
25  *
26  * This driver has been tested on a Fujitsu Lifebook S2110, S6410, S7020 and
27  * P8010.  It should work on most P-series and S-series Lifebooks, but
28  * YMMV.
29  *
30  * The module parameter use_alt_lcd_levels switches between different ACPI
31  * brightness controls which are used by different Fujitsu laptops.  In most
32  * cases the correct method is automatically detected. "use_alt_lcd_levels=1"
33  * is applicable for a Fujitsu Lifebook S6410 if autodetection fails.
34  *
35  */
36 
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38 
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/init.h>
42 #include <linux/acpi.h>
43 #include <linux/bitops.h>
44 #include <linux/dmi.h>
45 #include <linux/backlight.h>
46 #include <linux/fb.h>
47 #include <linux/input.h>
48 #include <linux/input/sparse-keymap.h>
49 #include <linux/kfifo.h>
50 #include <linux/leds.h>
51 #include <linux/platform_device.h>
52 #include <acpi/video.h>
53 
54 #define FUJITSU_DRIVER_VERSION		"0.6.0"
55 
56 #define FUJITSU_LCD_N_LEVELS		8
57 
58 #define ACPI_FUJITSU_CLASS		"fujitsu"
59 #define ACPI_FUJITSU_BL_HID		"FUJ02B1"
60 #define ACPI_FUJITSU_BL_DRIVER_NAME	"Fujitsu laptop FUJ02B1 ACPI brightness driver"
61 #define ACPI_FUJITSU_BL_DEVICE_NAME	"Fujitsu FUJ02B1"
62 #define ACPI_FUJITSU_LAPTOP_HID		"FUJ02E3"
63 #define ACPI_FUJITSU_LAPTOP_DRIVER_NAME	"Fujitsu laptop FUJ02E3 ACPI hotkeys driver"
64 #define ACPI_FUJITSU_LAPTOP_DEVICE_NAME	"Fujitsu FUJ02E3"
65 
66 #define ACPI_FUJITSU_NOTIFY_CODE	0x80
67 
68 /* FUNC interface - command values */
69 #define FUNC_FLAGS			BIT(12)
70 #define FUNC_LEDS			(BIT(12) | BIT(0))
71 #define FUNC_BUTTONS			(BIT(12) | BIT(1))
72 #define FUNC_BACKLIGHT			(BIT(12) | BIT(2))
73 
74 /* FUNC interface - responses */
75 #define UNSUPPORTED_CMD			0x80000000
76 
77 /* FUNC interface - status flags */
78 #define FLAG_RFKILL			BIT(5)
79 #define FLAG_LID			BIT(8)
80 #define FLAG_DOCK			BIT(9)
81 #define FLAG_TOUCHPAD_TOGGLE		BIT(26)
82 #define FLAG_MICMUTE			BIT(29)
83 #define FLAG_SOFTKEYS			(FLAG_RFKILL | FLAG_TOUCHPAD_TOGGLE | FLAG_MICMUTE)
84 
85 /* FUNC interface - LED control */
86 #define FUNC_LED_OFF			BIT(0)
87 #define FUNC_LED_ON			(BIT(0) | BIT(16) | BIT(17))
88 #define LOGOLAMP_POWERON		BIT(13)
89 #define LOGOLAMP_ALWAYS			BIT(14)
90 #define KEYBOARD_LAMPS			BIT(8)
91 #define RADIO_LED_ON			BIT(5)
92 #define ECO_LED				BIT(16)
93 #define ECO_LED_ON			BIT(19)
94 
95 /* FUNC interface - backlight power control */
96 #define BACKLIGHT_PARAM_POWER		BIT(2)
97 #define BACKLIGHT_OFF			(BIT(0) | BIT(1))
98 #define BACKLIGHT_ON			0
99 
100 /* Scancodes read from the GIRB register */
101 #define KEY1_CODE			0x410
102 #define KEY2_CODE			0x411
103 #define KEY3_CODE			0x412
104 #define KEY4_CODE			0x413
105 #define KEY5_CODE			0x414
106 #define KEY6_CODE			0x415
107 #define KEY7_CODE			0x416
108 #define KEY8_CODE			0x417
109 #define KEY9_CODE			0x420
110 
111 /* Hotkey ringbuffer limits */
112 #define MAX_HOTKEY_RINGBUFFER_SIZE	100
113 #define RINGBUFFERSIZE			40
114 
115 /* Module parameters */
116 static int use_alt_lcd_levels = -1;
117 static bool disable_brightness_adjust;
118 
119 /* Device controlling the backlight and associated keys */
120 struct fujitsu_bl {
121 	struct input_dev *input;
122 	char phys[32];
123 	struct backlight_device *bl_device;
124 	unsigned int max_brightness;
125 	unsigned int brightness_level;
126 };
127 
128 static struct fujitsu_bl *fujitsu_bl;
129 
130 /* Device used to access hotkeys and other features on the laptop */
131 struct fujitsu_laptop {
132 	struct input_dev *input;
133 	char phys[32];
134 	struct platform_device *pf_device;
135 	struct kfifo fifo;
136 	spinlock_t fifo_lock;
137 	int flags_supported;
138 	int flags_state;
139 };
140 
141 static struct acpi_device *fext;
142 
143 /* Fujitsu ACPI interface function */
144 
call_fext_func(struct acpi_device * device,int func,int op,int feature,int state)145 static int call_fext_func(struct acpi_device *device,
146 			  int func, int op, int feature, int state)
147 {
148 	union acpi_object params[4] = {
149 		{ .integer.type = ACPI_TYPE_INTEGER, .integer.value = func },
150 		{ .integer.type = ACPI_TYPE_INTEGER, .integer.value = op },
151 		{ .integer.type = ACPI_TYPE_INTEGER, .integer.value = feature },
152 		{ .integer.type = ACPI_TYPE_INTEGER, .integer.value = state }
153 	};
154 	struct acpi_object_list arg_list = { 4, params };
155 	unsigned long long value;
156 	acpi_status status;
157 
158 	status = acpi_evaluate_integer(device->handle, "FUNC", &arg_list,
159 				       &value);
160 	if (ACPI_FAILURE(status)) {
161 		acpi_handle_err(device->handle, "Failed to evaluate FUNC\n");
162 		return -ENODEV;
163 	}
164 
165 	acpi_handle_debug(device->handle,
166 			  "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) returned 0x%x\n",
167 			  func, op, feature, state, (int)value);
168 	return value;
169 }
170 
171 /* Hardware access for LCD brightness control */
172 
set_lcd_level(struct acpi_device * device,int level)173 static int set_lcd_level(struct acpi_device *device, int level)
174 {
175 	struct fujitsu_bl *priv = acpi_driver_data(device);
176 	acpi_status status;
177 	char *method;
178 
179 	switch (use_alt_lcd_levels) {
180 	case -1:
181 		if (acpi_has_method(device->handle, "SBL2"))
182 			method = "SBL2";
183 		else
184 			method = "SBLL";
185 		break;
186 	case 1:
187 		method = "SBL2";
188 		break;
189 	default:
190 		method = "SBLL";
191 		break;
192 	}
193 
194 	acpi_handle_debug(device->handle, "set lcd level via %s [%d]\n", method,
195 			  level);
196 
197 	if (level < 0 || level >= priv->max_brightness)
198 		return -EINVAL;
199 
200 	status = acpi_execute_simple_method(device->handle, method, level);
201 	if (ACPI_FAILURE(status)) {
202 		acpi_handle_err(device->handle, "Failed to evaluate %s\n",
203 				method);
204 		return -ENODEV;
205 	}
206 
207 	priv->brightness_level = level;
208 
209 	return 0;
210 }
211 
get_lcd_level(struct acpi_device * device)212 static int get_lcd_level(struct acpi_device *device)
213 {
214 	struct fujitsu_bl *priv = acpi_driver_data(device);
215 	unsigned long long state = 0;
216 	acpi_status status = AE_OK;
217 
218 	acpi_handle_debug(device->handle, "get lcd level via GBLL\n");
219 
220 	status = acpi_evaluate_integer(device->handle, "GBLL", NULL, &state);
221 	if (ACPI_FAILURE(status))
222 		return 0;
223 
224 	priv->brightness_level = state & 0x0fffffff;
225 
226 	return priv->brightness_level;
227 }
228 
get_max_brightness(struct acpi_device * device)229 static int get_max_brightness(struct acpi_device *device)
230 {
231 	struct fujitsu_bl *priv = acpi_driver_data(device);
232 	unsigned long long state = 0;
233 	acpi_status status = AE_OK;
234 
235 	acpi_handle_debug(device->handle, "get max lcd level via RBLL\n");
236 
237 	status = acpi_evaluate_integer(device->handle, "RBLL", NULL, &state);
238 	if (ACPI_FAILURE(status))
239 		return -1;
240 
241 	priv->max_brightness = state;
242 
243 	return priv->max_brightness;
244 }
245 
246 /* Backlight device stuff */
247 
bl_get_brightness(struct backlight_device * b)248 static int bl_get_brightness(struct backlight_device *b)
249 {
250 	struct acpi_device *device = bl_get_data(b);
251 
252 	return b->props.power == FB_BLANK_POWERDOWN ? 0 : get_lcd_level(device);
253 }
254 
bl_update_status(struct backlight_device * b)255 static int bl_update_status(struct backlight_device *b)
256 {
257 	struct acpi_device *device = bl_get_data(b);
258 
259 	if (fext) {
260 		if (b->props.power == FB_BLANK_POWERDOWN)
261 			call_fext_func(fext, FUNC_BACKLIGHT, 0x1,
262 				       BACKLIGHT_PARAM_POWER, BACKLIGHT_OFF);
263 		else
264 			call_fext_func(fext, FUNC_BACKLIGHT, 0x1,
265 				       BACKLIGHT_PARAM_POWER, BACKLIGHT_ON);
266 	}
267 
268 	return set_lcd_level(device, b->props.brightness);
269 }
270 
271 static const struct backlight_ops fujitsu_bl_ops = {
272 	.get_brightness = bl_get_brightness,
273 	.update_status = bl_update_status,
274 };
275 
lid_show(struct device * dev,struct device_attribute * attr,char * buf)276 static ssize_t lid_show(struct device *dev, struct device_attribute *attr,
277 			char *buf)
278 {
279 	struct fujitsu_laptop *priv = dev_get_drvdata(dev);
280 
281 	if (!(priv->flags_supported & FLAG_LID))
282 		return sprintf(buf, "unknown\n");
283 	if (priv->flags_state & FLAG_LID)
284 		return sprintf(buf, "open\n");
285 	else
286 		return sprintf(buf, "closed\n");
287 }
288 
dock_show(struct device * dev,struct device_attribute * attr,char * buf)289 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
290 			 char *buf)
291 {
292 	struct fujitsu_laptop *priv = dev_get_drvdata(dev);
293 
294 	if (!(priv->flags_supported & FLAG_DOCK))
295 		return sprintf(buf, "unknown\n");
296 	if (priv->flags_state & FLAG_DOCK)
297 		return sprintf(buf, "docked\n");
298 	else
299 		return sprintf(buf, "undocked\n");
300 }
301 
radios_show(struct device * dev,struct device_attribute * attr,char * buf)302 static ssize_t radios_show(struct device *dev, struct device_attribute *attr,
303 			   char *buf)
304 {
305 	struct fujitsu_laptop *priv = dev_get_drvdata(dev);
306 
307 	if (!(priv->flags_supported & FLAG_RFKILL))
308 		return sprintf(buf, "unknown\n");
309 	if (priv->flags_state & FLAG_RFKILL)
310 		return sprintf(buf, "on\n");
311 	else
312 		return sprintf(buf, "killed\n");
313 }
314 
315 static DEVICE_ATTR_RO(lid);
316 static DEVICE_ATTR_RO(dock);
317 static DEVICE_ATTR_RO(radios);
318 
319 static struct attribute *fujitsu_pf_attributes[] = {
320 	&dev_attr_lid.attr,
321 	&dev_attr_dock.attr,
322 	&dev_attr_radios.attr,
323 	NULL
324 };
325 
326 static const struct attribute_group fujitsu_pf_attribute_group = {
327 	.attrs = fujitsu_pf_attributes
328 };
329 
330 static struct platform_driver fujitsu_pf_driver = {
331 	.driver = {
332 		   .name = "fujitsu-laptop",
333 		   }
334 };
335 
336 /* ACPI device for LCD brightness control */
337 
338 static const struct key_entry keymap_backlight[] = {
339 	{ KE_KEY, true, { KEY_BRIGHTNESSUP } },
340 	{ KE_KEY, false, { KEY_BRIGHTNESSDOWN } },
341 	{ KE_END, 0 }
342 };
343 
acpi_fujitsu_bl_input_setup(struct acpi_device * device)344 static int acpi_fujitsu_bl_input_setup(struct acpi_device *device)
345 {
346 	struct fujitsu_bl *priv = acpi_driver_data(device);
347 	int ret;
348 
349 	priv->input = devm_input_allocate_device(&device->dev);
350 	if (!priv->input)
351 		return -ENOMEM;
352 
353 	snprintf(priv->phys, sizeof(priv->phys), "%s/video/input0",
354 		 acpi_device_hid(device));
355 
356 	priv->input->name = acpi_device_name(device);
357 	priv->input->phys = priv->phys;
358 	priv->input->id.bustype = BUS_HOST;
359 	priv->input->id.product = 0x06;
360 
361 	ret = sparse_keymap_setup(priv->input, keymap_backlight, NULL);
362 	if (ret)
363 		return ret;
364 
365 	return input_register_device(priv->input);
366 }
367 
fujitsu_backlight_register(struct acpi_device * device)368 static int fujitsu_backlight_register(struct acpi_device *device)
369 {
370 	struct fujitsu_bl *priv = acpi_driver_data(device);
371 	const struct backlight_properties props = {
372 		.brightness = priv->brightness_level,
373 		.max_brightness = priv->max_brightness - 1,
374 		.type = BACKLIGHT_PLATFORM
375 	};
376 	struct backlight_device *bd;
377 
378 	bd = devm_backlight_device_register(&device->dev, "fujitsu-laptop",
379 					    &device->dev, device,
380 					    &fujitsu_bl_ops, &props);
381 	if (IS_ERR(bd))
382 		return PTR_ERR(bd);
383 
384 	priv->bl_device = bd;
385 
386 	return 0;
387 }
388 
acpi_fujitsu_bl_add(struct acpi_device * device)389 static int acpi_fujitsu_bl_add(struct acpi_device *device)
390 {
391 	struct fujitsu_bl *priv;
392 	int ret;
393 
394 	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
395 		return -ENODEV;
396 
397 	priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
398 	if (!priv)
399 		return -ENOMEM;
400 
401 	fujitsu_bl = priv;
402 	strcpy(acpi_device_name(device), ACPI_FUJITSU_BL_DEVICE_NAME);
403 	strcpy(acpi_device_class(device), ACPI_FUJITSU_CLASS);
404 	device->driver_data = priv;
405 
406 	pr_info("ACPI: %s [%s]\n",
407 		acpi_device_name(device), acpi_device_bid(device));
408 
409 	if (get_max_brightness(device) <= 0)
410 		priv->max_brightness = FUJITSU_LCD_N_LEVELS;
411 	get_lcd_level(device);
412 
413 	ret = acpi_fujitsu_bl_input_setup(device);
414 	if (ret)
415 		return ret;
416 
417 	return fujitsu_backlight_register(device);
418 }
419 
420 /* Brightness notify */
421 
acpi_fujitsu_bl_notify(struct acpi_device * device,u32 event)422 static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
423 {
424 	struct fujitsu_bl *priv = acpi_driver_data(device);
425 	int oldb, newb;
426 
427 	if (event != ACPI_FUJITSU_NOTIFY_CODE) {
428 		acpi_handle_info(device->handle, "unsupported event [0x%x]\n",
429 				 event);
430 		sparse_keymap_report_event(priv->input, -1, 1, true);
431 		return;
432 	}
433 
434 	oldb = priv->brightness_level;
435 	get_lcd_level(device);
436 	newb = priv->brightness_level;
437 
438 	acpi_handle_debug(device->handle,
439 			  "brightness button event [%i -> %i]\n", oldb, newb);
440 
441 	if (oldb == newb)
442 		return;
443 
444 	if (!disable_brightness_adjust)
445 		set_lcd_level(device, newb);
446 
447 	sparse_keymap_report_event(priv->input, oldb < newb, 1, true);
448 }
449 
450 /* ACPI device for hotkey handling */
451 
452 static const struct key_entry keymap_default[] = {
453 	{ KE_KEY, KEY1_CODE,            { KEY_PROG1 } },
454 	{ KE_KEY, KEY2_CODE,            { KEY_PROG2 } },
455 	{ KE_KEY, KEY3_CODE,            { KEY_PROG3 } },
456 	{ KE_KEY, KEY4_CODE,            { KEY_PROG4 } },
457 	{ KE_KEY, KEY9_CODE,            { KEY_RFKILL } },
458 	/* Soft keys read from status flags */
459 	{ KE_KEY, FLAG_RFKILL,          { KEY_RFKILL } },
460 	{ KE_KEY, FLAG_TOUCHPAD_TOGGLE, { KEY_TOUCHPAD_TOGGLE } },
461 	{ KE_KEY, FLAG_MICMUTE,         { KEY_MICMUTE } },
462 	{ KE_END, 0 }
463 };
464 
465 static const struct key_entry keymap_s64x0[] = {
466 	{ KE_KEY, KEY1_CODE, { KEY_SCREENLOCK } },	/* "Lock" */
467 	{ KE_KEY, KEY2_CODE, { KEY_HELP } },		/* "Mobility Center */
468 	{ KE_KEY, KEY3_CODE, { KEY_PROG3 } },
469 	{ KE_KEY, KEY4_CODE, { KEY_PROG4 } },
470 	{ KE_END, 0 }
471 };
472 
473 static const struct key_entry keymap_p8010[] = {
474 	{ KE_KEY, KEY1_CODE, { KEY_HELP } },		/* "Support" */
475 	{ KE_KEY, KEY2_CODE, { KEY_PROG2 } },
476 	{ KE_KEY, KEY3_CODE, { KEY_SWITCHVIDEOMODE } },	/* "Presentation" */
477 	{ KE_KEY, KEY4_CODE, { KEY_WWW } },		/* "WWW" */
478 	{ KE_END, 0 }
479 };
480 
481 static const struct key_entry keymap_s2110[] = {
482 	{ KE_KEY, KEY1_CODE, { KEY_PROG1 } }, /* "A" */
483 	{ KE_KEY, KEY2_CODE, { KEY_PROG2 } }, /* "B" */
484 	{ KE_KEY, KEY3_CODE, { KEY_WWW } },   /* "Internet" */
485 	{ KE_KEY, KEY4_CODE, { KEY_EMAIL } }, /* "E-mail" */
486 	{ KE_KEY, KEY5_CODE, { KEY_STOPCD } },
487 	{ KE_KEY, KEY6_CODE, { KEY_PLAYPAUSE } },
488 	{ KE_KEY, KEY7_CODE, { KEY_PREVIOUSSONG } },
489 	{ KE_KEY, KEY8_CODE, { KEY_NEXTSONG } },
490 	{ KE_END, 0 }
491 };
492 
493 static const struct key_entry *keymap = keymap_default;
494 
fujitsu_laptop_dmi_keymap_override(const struct dmi_system_id * id)495 static int fujitsu_laptop_dmi_keymap_override(const struct dmi_system_id *id)
496 {
497 	pr_info("Identified laptop model '%s'\n", id->ident);
498 	keymap = id->driver_data;
499 	return 1;
500 }
501 
502 static const struct dmi_system_id fujitsu_laptop_dmi_table[] = {
503 	{
504 		.callback = fujitsu_laptop_dmi_keymap_override,
505 		.ident = "Fujitsu Siemens S6410",
506 		.matches = {
507 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
508 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
509 		},
510 		.driver_data = (void *)keymap_s64x0
511 	},
512 	{
513 		.callback = fujitsu_laptop_dmi_keymap_override,
514 		.ident = "Fujitsu Siemens S6420",
515 		.matches = {
516 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
517 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6420"),
518 		},
519 		.driver_data = (void *)keymap_s64x0
520 	},
521 	{
522 		.callback = fujitsu_laptop_dmi_keymap_override,
523 		.ident = "Fujitsu LifeBook P8010",
524 		.matches = {
525 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
526 			DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
527 		},
528 		.driver_data = (void *)keymap_p8010
529 	},
530 	{
531 		.callback = fujitsu_laptop_dmi_keymap_override,
532 		.ident = "Fujitsu LifeBook S2110",
533 		.matches = {
534 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
535 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S2110"),
536 		},
537 		.driver_data = (void *)keymap_s2110
538 	},
539 	{}
540 };
541 
acpi_fujitsu_laptop_input_setup(struct acpi_device * device)542 static int acpi_fujitsu_laptop_input_setup(struct acpi_device *device)
543 {
544 	struct fujitsu_laptop *priv = acpi_driver_data(device);
545 	int ret;
546 
547 	priv->input = devm_input_allocate_device(&device->dev);
548 	if (!priv->input)
549 		return -ENOMEM;
550 
551 	snprintf(priv->phys, sizeof(priv->phys), "%s/input0",
552 		 acpi_device_hid(device));
553 
554 	priv->input->name = acpi_device_name(device);
555 	priv->input->phys = priv->phys;
556 	priv->input->id.bustype = BUS_HOST;
557 
558 	dmi_check_system(fujitsu_laptop_dmi_table);
559 	ret = sparse_keymap_setup(priv->input, keymap, NULL);
560 	if (ret)
561 		return ret;
562 
563 	return input_register_device(priv->input);
564 }
565 
fujitsu_laptop_platform_add(struct acpi_device * device)566 static int fujitsu_laptop_platform_add(struct acpi_device *device)
567 {
568 	struct fujitsu_laptop *priv = acpi_driver_data(device);
569 	int ret;
570 
571 	priv->pf_device = platform_device_alloc("fujitsu-laptop", PLATFORM_DEVID_NONE);
572 	if (!priv->pf_device)
573 		return -ENOMEM;
574 
575 	platform_set_drvdata(priv->pf_device, priv);
576 
577 	ret = platform_device_add(priv->pf_device);
578 	if (ret)
579 		goto err_put_platform_device;
580 
581 	ret = sysfs_create_group(&priv->pf_device->dev.kobj,
582 				 &fujitsu_pf_attribute_group);
583 	if (ret)
584 		goto err_del_platform_device;
585 
586 	return 0;
587 
588 err_del_platform_device:
589 	platform_device_del(priv->pf_device);
590 err_put_platform_device:
591 	platform_device_put(priv->pf_device);
592 
593 	return ret;
594 }
595 
fujitsu_laptop_platform_remove(struct acpi_device * device)596 static void fujitsu_laptop_platform_remove(struct acpi_device *device)
597 {
598 	struct fujitsu_laptop *priv = acpi_driver_data(device);
599 
600 	sysfs_remove_group(&priv->pf_device->dev.kobj,
601 			   &fujitsu_pf_attribute_group);
602 	platform_device_unregister(priv->pf_device);
603 }
604 
logolamp_set(struct led_classdev * cdev,enum led_brightness brightness)605 static int logolamp_set(struct led_classdev *cdev,
606 			enum led_brightness brightness)
607 {
608 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
609 	int poweron = FUNC_LED_ON, always = FUNC_LED_ON;
610 	int ret;
611 
612 	if (brightness < LED_HALF)
613 		poweron = FUNC_LED_OFF;
614 
615 	if (brightness < LED_FULL)
616 		always = FUNC_LED_OFF;
617 
618 	ret = call_fext_func(device, FUNC_LEDS, 0x1, LOGOLAMP_POWERON, poweron);
619 	if (ret < 0)
620 		return ret;
621 
622 	return call_fext_func(device, FUNC_LEDS, 0x1, LOGOLAMP_ALWAYS, always);
623 }
624 
logolamp_get(struct led_classdev * cdev)625 static enum led_brightness logolamp_get(struct led_classdev *cdev)
626 {
627 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
628 	int ret;
629 
630 	ret = call_fext_func(device, FUNC_LEDS, 0x2, LOGOLAMP_ALWAYS, 0x0);
631 	if (ret == FUNC_LED_ON)
632 		return LED_FULL;
633 
634 	ret = call_fext_func(device, FUNC_LEDS, 0x2, LOGOLAMP_POWERON, 0x0);
635 	if (ret == FUNC_LED_ON)
636 		return LED_HALF;
637 
638 	return LED_OFF;
639 }
640 
kblamps_set(struct led_classdev * cdev,enum led_brightness brightness)641 static int kblamps_set(struct led_classdev *cdev,
642 		       enum led_brightness brightness)
643 {
644 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
645 
646 	if (brightness >= LED_FULL)
647 		return call_fext_func(device, FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
648 				      FUNC_LED_ON);
649 	else
650 		return call_fext_func(device, FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
651 				      FUNC_LED_OFF);
652 }
653 
kblamps_get(struct led_classdev * cdev)654 static enum led_brightness kblamps_get(struct led_classdev *cdev)
655 {
656 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
657 	enum led_brightness brightness = LED_OFF;
658 
659 	if (call_fext_func(device,
660 			   FUNC_LEDS, 0x2, KEYBOARD_LAMPS, 0x0) == FUNC_LED_ON)
661 		brightness = LED_FULL;
662 
663 	return brightness;
664 }
665 
radio_led_set(struct led_classdev * cdev,enum led_brightness brightness)666 static int radio_led_set(struct led_classdev *cdev,
667 			 enum led_brightness brightness)
668 {
669 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
670 
671 	if (brightness >= LED_FULL)
672 		return call_fext_func(device, FUNC_FLAGS, 0x5, RADIO_LED_ON,
673 				      RADIO_LED_ON);
674 	else
675 		return call_fext_func(device, FUNC_FLAGS, 0x5, RADIO_LED_ON,
676 				      0x0);
677 }
678 
radio_led_get(struct led_classdev * cdev)679 static enum led_brightness radio_led_get(struct led_classdev *cdev)
680 {
681 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
682 	enum led_brightness brightness = LED_OFF;
683 
684 	if (call_fext_func(device, FUNC_FLAGS, 0x4, 0x0, 0x0) & RADIO_LED_ON)
685 		brightness = LED_FULL;
686 
687 	return brightness;
688 }
689 
eco_led_set(struct led_classdev * cdev,enum led_brightness brightness)690 static int eco_led_set(struct led_classdev *cdev,
691 		       enum led_brightness brightness)
692 {
693 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
694 	int curr;
695 
696 	curr = call_fext_func(device, FUNC_LEDS, 0x2, ECO_LED, 0x0);
697 	if (brightness >= LED_FULL)
698 		return call_fext_func(device, FUNC_LEDS, 0x1, ECO_LED,
699 				      curr | ECO_LED_ON);
700 	else
701 		return call_fext_func(device, FUNC_LEDS, 0x1, ECO_LED,
702 				      curr & ~ECO_LED_ON);
703 }
704 
eco_led_get(struct led_classdev * cdev)705 static enum led_brightness eco_led_get(struct led_classdev *cdev)
706 {
707 	struct acpi_device *device = to_acpi_device(cdev->dev->parent);
708 	enum led_brightness brightness = LED_OFF;
709 
710 	if (call_fext_func(device, FUNC_LEDS, 0x2, ECO_LED, 0x0) & ECO_LED_ON)
711 		brightness = LED_FULL;
712 
713 	return brightness;
714 }
715 
acpi_fujitsu_laptop_leds_register(struct acpi_device * device)716 static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device)
717 {
718 	struct fujitsu_laptop *priv = acpi_driver_data(device);
719 	struct led_classdev *led;
720 	int ret;
721 
722 	if (call_fext_func(device,
723 			   FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
724 		led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
725 		if (!led)
726 			return -ENOMEM;
727 
728 		led->name = "fujitsu::logolamp";
729 		led->brightness_set_blocking = logolamp_set;
730 		led->brightness_get = logolamp_get;
731 		ret = devm_led_classdev_register(&device->dev, led);
732 		if (ret)
733 			return ret;
734 	}
735 
736 	if ((call_fext_func(device,
737 			    FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
738 	    (call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
739 		led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
740 		if (!led)
741 			return -ENOMEM;
742 
743 		led->name = "fujitsu::kblamps";
744 		led->brightness_set_blocking = kblamps_set;
745 		led->brightness_get = kblamps_get;
746 		ret = devm_led_classdev_register(&device->dev, led);
747 		if (ret)
748 			return ret;
749 	}
750 
751 	/*
752 	 * Some Fujitsu laptops have a radio toggle button in place of a slide
753 	 * switch and all such machines appear to also have an RF LED.  Based on
754 	 * comparing DSDT tables of four Fujitsu Lifebook models (E744, E751,
755 	 * S7110, S8420; the first one has a radio toggle button, the other
756 	 * three have slide switches), bit 17 of flags_supported (the value
757 	 * returned by method S000 of ACPI device FUJ02E3) seems to indicate
758 	 * whether given model has a radio toggle button.
759 	 */
760 	if (priv->flags_supported & BIT(17)) {
761 		led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
762 		if (!led)
763 			return -ENOMEM;
764 
765 		led->name = "fujitsu::radio_led";
766 		led->brightness_set_blocking = radio_led_set;
767 		led->brightness_get = radio_led_get;
768 		led->default_trigger = "rfkill-any";
769 		ret = devm_led_classdev_register(&device->dev, led);
770 		if (ret)
771 			return ret;
772 	}
773 
774 	/* Support for eco led is not always signaled in bit corresponding
775 	 * to the bit used to control the led. According to the DSDT table,
776 	 * bit 14 seems to indicate presence of said led as well.
777 	 * Confirm by testing the status.
778 	 */
779 	if ((call_fext_func(device, FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
780 	    (call_fext_func(device,
781 			    FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
782 		led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
783 		if (!led)
784 			return -ENOMEM;
785 
786 		led->name = "fujitsu::eco_led";
787 		led->brightness_set_blocking = eco_led_set;
788 		led->brightness_get = eco_led_get;
789 		ret = devm_led_classdev_register(&device->dev, led);
790 		if (ret)
791 			return ret;
792 	}
793 
794 	return 0;
795 }
796 
acpi_fujitsu_laptop_add(struct acpi_device * device)797 static int acpi_fujitsu_laptop_add(struct acpi_device *device)
798 {
799 	struct fujitsu_laptop *priv;
800 	int ret, i = 0;
801 
802 	priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
803 	if (!priv)
804 		return -ENOMEM;
805 
806 	WARN_ONCE(fext, "More than one FUJ02E3 ACPI device was found.  Driver may not work as intended.");
807 	fext = device;
808 
809 	strcpy(acpi_device_name(device), ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
810 	strcpy(acpi_device_class(device), ACPI_FUJITSU_CLASS);
811 	device->driver_data = priv;
812 
813 	/* kfifo */
814 	spin_lock_init(&priv->fifo_lock);
815 	ret = kfifo_alloc(&priv->fifo, RINGBUFFERSIZE * sizeof(int),
816 			  GFP_KERNEL);
817 	if (ret)
818 		return ret;
819 
820 	pr_info("ACPI: %s [%s]\n",
821 		acpi_device_name(device), acpi_device_bid(device));
822 
823 	while (call_fext_func(device, FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0 &&
824 	       i++ < MAX_HOTKEY_RINGBUFFER_SIZE)
825 		; /* No action, result is discarded */
826 	acpi_handle_debug(device->handle, "Discarded %i ringbuffer entries\n",
827 			  i);
828 
829 	priv->flags_supported = call_fext_func(device, FUNC_FLAGS, 0x0, 0x0,
830 					       0x0);
831 
832 	/* Make sure our bitmask of supported functions is cleared if the
833 	   RFKILL function block is not implemented, like on the S7020. */
834 	if (priv->flags_supported == UNSUPPORTED_CMD)
835 		priv->flags_supported = 0;
836 
837 	if (priv->flags_supported)
838 		priv->flags_state = call_fext_func(device, FUNC_FLAGS, 0x4, 0x0,
839 						   0x0);
840 
841 	/* Suspect this is a keymap of the application panel, print it */
842 	acpi_handle_info(device->handle, "BTNI: [0x%x]\n",
843 			 call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0));
844 
845 	/* Sync backlight power status */
846 	if (fujitsu_bl && fujitsu_bl->bl_device &&
847 	    acpi_video_get_backlight_type() == acpi_backlight_vendor) {
848 		if (call_fext_func(fext, FUNC_BACKLIGHT, 0x2,
849 				   BACKLIGHT_PARAM_POWER, 0x0) == BACKLIGHT_OFF)
850 			fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
851 		else
852 			fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
853 	}
854 
855 	ret = acpi_fujitsu_laptop_input_setup(device);
856 	if (ret)
857 		goto err_free_fifo;
858 
859 	ret = acpi_fujitsu_laptop_leds_register(device);
860 	if (ret)
861 		goto err_free_fifo;
862 
863 	ret = fujitsu_laptop_platform_add(device);
864 	if (ret)
865 		goto err_free_fifo;
866 
867 	return 0;
868 
869 err_free_fifo:
870 	kfifo_free(&priv->fifo);
871 
872 	return ret;
873 }
874 
acpi_fujitsu_laptop_remove(struct acpi_device * device)875 static void acpi_fujitsu_laptop_remove(struct acpi_device *device)
876 {
877 	struct fujitsu_laptop *priv = acpi_driver_data(device);
878 
879 	fujitsu_laptop_platform_remove(device);
880 
881 	kfifo_free(&priv->fifo);
882 }
883 
acpi_fujitsu_laptop_press(struct acpi_device * device,int scancode)884 static void acpi_fujitsu_laptop_press(struct acpi_device *device, int scancode)
885 {
886 	struct fujitsu_laptop *priv = acpi_driver_data(device);
887 	int ret;
888 
889 	ret = kfifo_in_locked(&priv->fifo, (unsigned char *)&scancode,
890 			      sizeof(scancode), &priv->fifo_lock);
891 	if (ret != sizeof(scancode)) {
892 		dev_info(&priv->input->dev, "Could not push scancode [0x%x]\n",
893 			 scancode);
894 		return;
895 	}
896 	sparse_keymap_report_event(priv->input, scancode, 1, false);
897 	dev_dbg(&priv->input->dev, "Push scancode into ringbuffer [0x%x]\n",
898 		scancode);
899 }
900 
acpi_fujitsu_laptop_release(struct acpi_device * device)901 static void acpi_fujitsu_laptop_release(struct acpi_device *device)
902 {
903 	struct fujitsu_laptop *priv = acpi_driver_data(device);
904 	int scancode, ret;
905 
906 	while (true) {
907 		ret = kfifo_out_locked(&priv->fifo, (unsigned char *)&scancode,
908 				       sizeof(scancode), &priv->fifo_lock);
909 		if (ret != sizeof(scancode))
910 			return;
911 		sparse_keymap_report_event(priv->input, scancode, 0, false);
912 		dev_dbg(&priv->input->dev,
913 			"Pop scancode from ringbuffer [0x%x]\n", scancode);
914 	}
915 }
916 
acpi_fujitsu_laptop_notify(struct acpi_device * device,u32 event)917 static void acpi_fujitsu_laptop_notify(struct acpi_device *device, u32 event)
918 {
919 	struct fujitsu_laptop *priv = acpi_driver_data(device);
920 	unsigned long flags;
921 	int scancode, i = 0;
922 	unsigned int irb;
923 
924 	if (event != ACPI_FUJITSU_NOTIFY_CODE) {
925 		acpi_handle_info(device->handle, "Unsupported event [0x%x]\n",
926 				 event);
927 		sparse_keymap_report_event(priv->input, -1, 1, true);
928 		return;
929 	}
930 
931 	if (priv->flags_supported)
932 		priv->flags_state = call_fext_func(device, FUNC_FLAGS, 0x4, 0x0,
933 						   0x0);
934 
935 	while ((irb = call_fext_func(device,
936 				     FUNC_BUTTONS, 0x1, 0x0, 0x0)) != 0 &&
937 	       i++ < MAX_HOTKEY_RINGBUFFER_SIZE) {
938 		scancode = irb & 0x4ff;
939 		if (sparse_keymap_entry_from_scancode(priv->input, scancode))
940 			acpi_fujitsu_laptop_press(device, scancode);
941 		else if (scancode == 0)
942 			acpi_fujitsu_laptop_release(device);
943 		else
944 			acpi_handle_info(device->handle,
945 					 "Unknown GIRB result [%x]\n", irb);
946 	}
947 
948 	/*
949 	 * First seen on the Skylake-based Lifebook E736/E746/E756), the
950 	 * touchpad toggle hotkey (Fn+F4) is handled in software. Other models
951 	 * have since added additional "soft keys". These are reported in the
952 	 * status flags queried using FUNC_FLAGS.
953 	 */
954 	if (priv->flags_supported & (FLAG_SOFTKEYS)) {
955 		flags = call_fext_func(device, FUNC_FLAGS, 0x1, 0x0, 0x0);
956 		flags &= (FLAG_SOFTKEYS);
957 		for_each_set_bit(i, &flags, BITS_PER_LONG)
958 			sparse_keymap_report_event(priv->input, BIT(i), 1, true);
959 	}
960 }
961 
962 /* Initialization */
963 
964 static const struct acpi_device_id fujitsu_bl_device_ids[] = {
965 	{ACPI_FUJITSU_BL_HID, 0},
966 	{"", 0},
967 };
968 
969 static struct acpi_driver acpi_fujitsu_bl_driver = {
970 	.name = ACPI_FUJITSU_BL_DRIVER_NAME,
971 	.class = ACPI_FUJITSU_CLASS,
972 	.ids = fujitsu_bl_device_ids,
973 	.ops = {
974 		.add = acpi_fujitsu_bl_add,
975 		.notify = acpi_fujitsu_bl_notify,
976 		},
977 };
978 
979 static const struct acpi_device_id fujitsu_laptop_device_ids[] = {
980 	{ACPI_FUJITSU_LAPTOP_HID, 0},
981 	{"", 0},
982 };
983 
984 static struct acpi_driver acpi_fujitsu_laptop_driver = {
985 	.name = ACPI_FUJITSU_LAPTOP_DRIVER_NAME,
986 	.class = ACPI_FUJITSU_CLASS,
987 	.ids = fujitsu_laptop_device_ids,
988 	.ops = {
989 		.add = acpi_fujitsu_laptop_add,
990 		.remove = acpi_fujitsu_laptop_remove,
991 		.notify = acpi_fujitsu_laptop_notify,
992 		},
993 };
994 
995 static const struct acpi_device_id fujitsu_ids[] __used = {
996 	{ACPI_FUJITSU_BL_HID, 0},
997 	{ACPI_FUJITSU_LAPTOP_HID, 0},
998 	{"", 0}
999 };
1000 MODULE_DEVICE_TABLE(acpi, fujitsu_ids);
1001 
fujitsu_init(void)1002 static int __init fujitsu_init(void)
1003 {
1004 	int ret;
1005 
1006 	ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
1007 	if (ret)
1008 		return ret;
1009 
1010 	/* Register platform stuff */
1011 
1012 	ret = platform_driver_register(&fujitsu_pf_driver);
1013 	if (ret)
1014 		goto err_unregister_acpi;
1015 
1016 	/* Register laptop driver */
1017 
1018 	ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
1019 	if (ret)
1020 		goto err_unregister_platform_driver;
1021 
1022 	pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
1023 
1024 	return 0;
1025 
1026 err_unregister_platform_driver:
1027 	platform_driver_unregister(&fujitsu_pf_driver);
1028 err_unregister_acpi:
1029 	acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
1030 
1031 	return ret;
1032 }
1033 
fujitsu_cleanup(void)1034 static void __exit fujitsu_cleanup(void)
1035 {
1036 	acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
1037 
1038 	platform_driver_unregister(&fujitsu_pf_driver);
1039 
1040 	acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
1041 
1042 	pr_info("driver unloaded\n");
1043 }
1044 
1045 module_init(fujitsu_init);
1046 module_exit(fujitsu_cleanup);
1047 
1048 module_param(use_alt_lcd_levels, int, 0644);
1049 MODULE_PARM_DESC(use_alt_lcd_levels, "Interface used for setting LCD brightness level (-1 = auto, 0 = force SBLL, 1 = force SBL2)");
1050 module_param(disable_brightness_adjust, bool, 0644);
1051 MODULE_PARM_DESC(disable_brightness_adjust, "Disable LCD brightness adjustment");
1052 
1053 MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
1054 MODULE_DESCRIPTION("Fujitsu laptop extras support");
1055 MODULE_VERSION(FUJITSU_DRIVER_VERSION);
1056 MODULE_LICENSE("GPL");
1057