blob: 22909063e9c4f1bd379c7c1c4f3d18193d58a583 [file] [log] [blame]
Jonathan Woithed0482532007-08-29 15:58:19 +09301/*-*-linux-c-*-*/
2
3/*
Jonathan Woithe409a3e92012-03-27 13:01:01 +10304 Copyright (C) 2007,2008 Jonathan Woithe <jwoithe@just42.net>
Jonathan Woithe20b93732008-06-11 10:14:56 +09305 Copyright (C) 2008 Peter Gruber <nokos@gmx.net>
Tony Vroon3a407082008-12-31 18:19:59 +00006 Copyright (C) 2008 Tony Vroon <tony@linx.net>
Jonathan Woithed0482532007-08-29 15:58:19 +09307 Based on earlier work:
8 Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
9 Adrian Yee <brewt-fujitsu@brewt.org>
10
Jonathan Woithe20b93732008-06-11 10:14:56 +093011 Templated from msi-laptop.c and thinkpad_acpi.c which is copyright
12 by its respective authors.
Jonathan Woithed0482532007-08-29 15:58:19 +093013
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 *
Michał Kępień78b26022017-03-14 11:26:27 +010035 * This driver implements a vendor-specific backlight control interface for
36 * Fujitsu laptops and provides support for hotkeys present on certain Fujitsu
37 * laptops.
Jonathan Woithe20b93732008-06-11 10:14:56 +093038 *
Jonathan Woithe0e6a66e2008-10-09 13:44:40 +093039 * 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.
Jonathan Woithe20b93732008-06-11 10:14:56 +093042 *
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 *
Jonathan Woithed0482532007-08-29 15:58:19 +093048 */
49
Joe Perches77bad7c2011-03-29 15:21:39 -070050#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51
Jonathan Woithed0482532007-08-29 15:58:19 +093052#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>
Michael Karchere8549e22015-01-18 20:28:46 +010058#include <linux/fb.h>
Jonathan Woithe20b93732008-06-11 10:14:56 +093059#include <linux/input.h>
Michał Kępieńf2252672017-03-20 10:32:19 +010060#include <linux/input/sparse-keymap.h>
Jonathan Woithe20b93732008-06-11 10:14:56 +093061#include <linux/kfifo.h>
Michał Kępieńd89bcc82017-04-07 15:07:08 +020062#include <linux/leds.h>
Jonathan Woithed0482532007-08-29 15:58:19 +093063#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/slab.h>
Hans de Goede413226f2015-06-16 16:28:03 +020065#include <acpi/video.h>
Jonathan Woithed0482532007-08-29 15:58:19 +093066
Jonathan Woithe84a6ce22009-07-31 18:16:59 +093067#define FUJITSU_DRIVER_VERSION "0.6.0"
Jonathan Woithed0482532007-08-29 15:58:19 +093068
69#define FUJITSU_LCD_N_LEVELS 8
70
Alan Jenkins9fc5cf62017-02-08 14:46:24 +010071#define ACPI_FUJITSU_CLASS "fujitsu"
72#define ACPI_FUJITSU_BL_HID "FUJ02B1"
73#define ACPI_FUJITSU_BL_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI brightness driver"
74#define ACPI_FUJITSU_BL_DEVICE_NAME "Fujitsu FUJ02B1"
Alan Jenkins6942eab2017-02-08 14:46:25 +010075#define ACPI_FUJITSU_LAPTOP_HID "FUJ02E3"
76#define ACPI_FUJITSU_LAPTOP_DRIVER_NAME "Fujitsu laptop FUJ02E3 ACPI hotkeys driver"
77#define ACPI_FUJITSU_LAPTOP_DEVICE_NAME "Fujitsu FUJ02E3"
Jonathan Woithed0482532007-08-29 15:58:19 +093078
Jonathan Woithe20b93732008-06-11 10:14:56 +093079#define ACPI_FUJITSU_NOTIFY_CODE1 0x80
80
Tony Vroon3a407082008-12-31 18:19:59 +000081/* FUNC interface - command values */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +010082#define FUNC_FLAGS 0x1000
Tony Vroon3a407082008-12-31 18:19:59 +000083#define FUNC_LEDS 0x1001
84#define FUNC_BUTTONS 0x1002
85#define FUNC_BACKLIGHT 0x1004
86
87/* FUNC interface - responses */
88#define UNSUPPORTED_CMD 0x80000000
89
Alan Jenkinsd3dd4482017-02-08 14:46:28 +010090/* FUNC interface - status flags */
91#define FLAG_RFKILL 0x020
92#define FLAG_LID 0x100
93#define FLAG_DOCK 0x200
94
Tony Vroon3a407082008-12-31 18:19:59 +000095/* FUNC interface - LED control */
96#define FUNC_LED_OFF 0x1
97#define FUNC_LED_ON 0x30001
98#define KEYBOARD_LAMPS 0x100
99#define LOGOLAMP_POWERON 0x2000
100#define LOGOLAMP_ALWAYS 0x4000
Michał Kępień4f625682016-04-12 22:06:34 +0930101#define RADIO_LED_ON 0x20
Matej Gromad6b88f62016-06-21 10:09:21 +0200102#define ECO_LED 0x10000
103#define ECO_LED_ON 0x80000
Tony Vroon3a407082008-12-31 18:19:59 +0000104
Jonathan Woithe20b93732008-06-11 10:14:56 +0930105/* Hotkey details */
Jonathan Woithe0e6a66e2008-10-09 13:44:40 +0930106#define KEY1_CODE 0x410 /* codes for the keys in the GIRB register */
107#define KEY2_CODE 0x411
108#define KEY3_CODE 0x412
109#define KEY4_CODE 0x413
Michał Kępieńb5df36c2016-02-24 14:23:32 +0100110#define KEY5_CODE 0x420
Jonathan Woithe20b93732008-06-11 10:14:56 +0930111
112#define MAX_HOTKEY_RINGBUFFER_SIZE 100
113#define RINGBUFFERSIZE 40
114
115/* Debugging */
Jonathan Woithe20b93732008-06-11 10:14:56 +0930116#define FUJLAPTOP_DBG_ERROR 0x0001
117#define FUJLAPTOP_DBG_WARN 0x0002
118#define FUJLAPTOP_DBG_INFO 0x0004
119#define FUJLAPTOP_DBG_TRACE 0x0008
120
Jean Delvarec4960cf2014-06-16 11:55:13 +0200121#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
122#define vdbg_printk(a_dbg_level, format, arg...) \
Jonathan Woithe20b93732008-06-11 10:14:56 +0930123 do { if (dbg_level & a_dbg_level) \
Michał Kępień98020a42016-06-23 12:02:47 +0200124 printk(KERN_DEBUG pr_fmt("%s: " format), __func__, ## arg); \
Jonathan Woithe20b93732008-06-11 10:14:56 +0930125 } while (0)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930126#else
Jean Delvarec4960cf2014-06-16 11:55:13 +0200127#define vdbg_printk(a_dbg_level, format, arg...) \
128 do { } while (0)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930129#endif
130
131/* Device controlling the backlight and associated keys */
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100132struct fujitsu_bl {
Jonathan Woithed0482532007-08-29 15:58:19 +0930133 acpi_handle acpi_handle;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930134 struct input_dev *input;
135 char phys[32];
Jonathan Woithed0482532007-08-29 15:58:19 +0930136 struct backlight_device *bl_device;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930137 unsigned int max_brightness;
Jonathan Woithed0482532007-08-29 15:58:19 +0930138 unsigned int brightness_level;
139};
140
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100141static struct fujitsu_bl *fujitsu_bl;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930142static int use_alt_lcd_levels = -1;
Michał Kępieńb4bb0cf2017-04-05 08:49:06 +0200143static bool disable_brightness_adjust;
Jonathan Woithed0482532007-08-29 15:58:19 +0930144
Alan Jenkins6942eab2017-02-08 14:46:25 +0100145/* Device used to access hotkeys and other features on the laptop */
146struct fujitsu_laptop {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930147 acpi_handle acpi_handle;
148 struct acpi_device *dev;
149 struct input_dev *input;
150 char phys[32];
151 struct platform_device *pf_device;
Stefani Seibold45465482009-12-21 14:37:26 -0800152 struct kfifo fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930153 spinlock_t fifo_lock;
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100154 int flags_supported;
155 int flags_state;
Tony Vroon3a407082008-12-31 18:19:59 +0000156 int logolamp_registered;
157 int kblamps_registered;
Michał Kępień4f625682016-04-12 22:06:34 +0930158 int radio_led_registered;
Matej Gromad6b88f62016-06-21 10:09:21 +0200159 int eco_led_registered;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930160};
161
Alan Jenkins6942eab2017-02-08 14:46:25 +0100162static struct fujitsu_laptop *fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930163
Jonathan Woithe20b93732008-06-11 10:14:56 +0930164#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
165static u32 dbg_level = 0x03;
166#endif
167
Tony Vroon3a407082008-12-31 18:19:59 +0000168/* Fujitsu ACPI interface function */
169
Michał Kępieńf68e4922017-04-03 11:38:59 +0200170static int call_fext_func(int func, int op, int feature, int state)
Tony Vroon3a407082008-12-31 18:19:59 +0000171{
Tony Vroon3a407082008-12-31 18:19:59 +0000172 union acpi_object params[4] = {
Michał Kępieńf68e4922017-04-03 11:38:59 +0200173 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = func },
174 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = op },
175 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = feature },
176 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = state }
Tony Vroon3a407082008-12-31 18:19:59 +0000177 };
Michał Kępieńb1066412017-04-03 11:38:57 +0200178 struct acpi_object_list arg_list = { 4, params };
Zhang Rui29c29a92013-09-03 08:32:12 +0800179 unsigned long long value;
Michał Kępieńb1066412017-04-03 11:38:57 +0200180 acpi_status status;
Tony Vroon3a407082008-12-31 18:19:59 +0000181
Michał Kępień17e23552017-04-03 11:38:58 +0200182 status = acpi_evaluate_integer(fujitsu_laptop->acpi_handle, "FUNC",
183 &arg_list, &value);
Tony Vroon3a407082008-12-31 18:19:59 +0000184 if (ACPI_FAILURE(status)) {
Michał Kępień09b29e12017-04-06 08:46:10 +0200185 vdbg_printk(FUJLAPTOP_DBG_ERROR, "Failed to evaluate FUNC\n");
Tony Vroon3a407082008-12-31 18:19:59 +0000186 return -ENODEV;
187 }
188
Michał Kępieńf68e4922017-04-03 11:38:59 +0200189 vdbg_printk(FUJLAPTOP_DBG_TRACE, "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) returned 0x%x\n",
190 func, op, feature, state, (int)value);
Zhang Rui29c29a92013-09-03 08:32:12 +0800191 return value;
Tony Vroon3a407082008-12-31 18:19:59 +0000192}
193
Jonathan Woithe20b93732008-06-11 10:14:56 +0930194/* Hardware access for LCD brightness control */
Jonathan Woithed0482532007-08-29 15:58:19 +0930195
196static int set_lcd_level(int level)
197{
Michał Kępieńa8779c32017-04-05 08:49:03 +0200198 acpi_status status;
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200199 char *method;
Jonathan Woithed0482532007-08-29 15:58:19 +0930200
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200201 switch (use_alt_lcd_levels) {
Michał Kępieńe06e4832017-04-05 08:49:05 +0200202 case -1:
203 if (acpi_has_method(fujitsu_bl->acpi_handle, "SBL2"))
204 method = "SBL2";
205 else
206 method = "SBLL";
207 break;
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200208 case 1:
209 method = "SBL2";
210 break;
211 default:
212 method = "SBLL";
213 break;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930214 }
215
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200216 vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via %s [%d]\n",
217 method, level);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930218
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100219 if (level < 0 || level >= fujitsu_bl->max_brightness)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930220 return -EINVAL;
221
Michał Kępieńa8779c32017-04-05 08:49:03 +0200222 status = acpi_execute_simple_method(fujitsu_bl->acpi_handle, method,
223 level);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930224 if (ACPI_FAILURE(status)) {
Michał Kępieńa8779c32017-04-05 08:49:03 +0200225 vdbg_printk(FUJLAPTOP_DBG_ERROR, "Failed to evaluate %s\n",
226 method);
Jonathan Woithed0482532007-08-29 15:58:19 +0930227 return -ENODEV;
228 }
229
Michał Kępieńbd079a22017-04-05 08:49:04 +0200230 fujitsu_bl->brightness_level = level;
231
Jonathan Woithed0482532007-08-29 15:58:19 +0930232 return 0;
233}
234
235static int get_lcd_level(void)
236{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400237 unsigned long long state = 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930238 acpi_status status = AE_OK;
239
Jonathan Woithe20b93732008-06-11 10:14:56 +0930240 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLL\n");
241
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100242 status = acpi_evaluate_integer(fujitsu_bl->acpi_handle, "GBLL", NULL,
243 &state);
Jonathan Woithe3b1c37c2009-12-23 09:19:42 +1030244 if (ACPI_FAILURE(status))
245 return 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930246
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100247 fujitsu_bl->brightness_level = state & 0x0fffffff;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930248
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100249 return fujitsu_bl->brightness_level;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930250}
251
252static int get_max_brightness(void)
253{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400254 unsigned long long state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930255 acpi_status status = AE_OK;
256
257 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get max lcd level via RBLL\n");
258
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100259 status = acpi_evaluate_integer(fujitsu_bl->acpi_handle, "RBLL", NULL,
260 &state);
Jonathan Woithe3b1c37c2009-12-23 09:19:42 +1030261 if (ACPI_FAILURE(status))
262 return -1;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930263
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100264 fujitsu_bl->max_brightness = state;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930265
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100266 return fujitsu_bl->max_brightness;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930267}
268
Jonathan Woithed0482532007-08-29 15:58:19 +0930269/* Backlight device stuff */
270
271static int bl_get_brightness(struct backlight_device *b)
272{
Michał Kępień5959ddd2017-04-05 08:49:09 +0200273 return b->props.power == FB_BLANK_POWERDOWN ? 0 : get_lcd_level();
Jonathan Woithed0482532007-08-29 15:58:19 +0930274}
275
276static int bl_update_status(struct backlight_device *b)
277{
Michael Karchere8549e22015-01-18 20:28:46 +0100278 if (b->props.power == FB_BLANK_POWERDOWN)
Michał Kępieńf7c4c3f2017-04-05 08:49:07 +0200279 call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x3);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930280 else
Michał Kępieńf7c4c3f2017-04-05 08:49:07 +0200281 call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x0);
Tony Vroon3a407082008-12-31 18:19:59 +0000282
Michał Kępieńd75a4a92017-04-05 08:49:08 +0200283 return set_lcd_level(b->props.brightness);
Jonathan Woithed0482532007-08-29 15:58:19 +0930284}
285
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100286static const struct backlight_ops fujitsu_bl_ops = {
Jonathan Woithed0482532007-08-29 15:58:19 +0930287 .get_brightness = bl_get_brightness,
288 .update_status = bl_update_status,
289};
290
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100291static ssize_t lid_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000293{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100294 if (!(fujitsu_laptop->flags_supported & FLAG_LID))
Tony Vroon3a407082008-12-31 18:19:59 +0000295 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100296 if (fujitsu_laptop->flags_state & FLAG_LID)
Tony Vroon3a407082008-12-31 18:19:59 +0000297 return sprintf(buf, "open\n");
298 else
299 return sprintf(buf, "closed\n");
300}
301
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100302static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
303 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000304{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100305 if (!(fujitsu_laptop->flags_supported & FLAG_DOCK))
Tony Vroon3a407082008-12-31 18:19:59 +0000306 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100307 if (fujitsu_laptop->flags_state & FLAG_DOCK)
Tony Vroon3a407082008-12-31 18:19:59 +0000308 return sprintf(buf, "docked\n");
309 else
310 return sprintf(buf, "undocked\n");
311}
312
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100313static ssize_t radios_show(struct device *dev, struct device_attribute *attr,
314 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000315{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100316 if (!(fujitsu_laptop->flags_supported & FLAG_RFKILL))
Tony Vroon3a407082008-12-31 18:19:59 +0000317 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100318 if (fujitsu_laptop->flags_state & FLAG_RFKILL)
Tony Vroon3a407082008-12-31 18:19:59 +0000319 return sprintf(buf, "on\n");
320 else
321 return sprintf(buf, "killed\n");
322}
323
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100324static DEVICE_ATTR_RO(lid);
325static DEVICE_ATTR_RO(dock);
326static DEVICE_ATTR_RO(radios);
Jonathan Woithed0482532007-08-29 15:58:19 +0930327
Alan Jenkins16506022017-02-08 14:46:26 +0100328static struct attribute *fujitsu_pf_attributes[] = {
Tony Vroon3a407082008-12-31 18:19:59 +0000329 &dev_attr_lid.attr,
330 &dev_attr_dock.attr,
331 &dev_attr_radios.attr,
Jonathan Woithed0482532007-08-29 15:58:19 +0930332 NULL
333};
334
Alan Jenkins16506022017-02-08 14:46:26 +0100335static struct attribute_group fujitsu_pf_attribute_group = {
336 .attrs = fujitsu_pf_attributes
Jonathan Woithed0482532007-08-29 15:58:19 +0930337};
338
Alan Jenkins16506022017-02-08 14:46:26 +0100339static struct platform_driver fujitsu_pf_driver = {
Jonathan Woithed0482532007-08-29 15:58:19 +0930340 .driver = {
341 .name = "fujitsu-laptop",
Jonathan Woithed0482532007-08-29 15:58:19 +0930342 }
343};
344
Jonathan Woithe20b93732008-06-11 10:14:56 +0930345/* ACPI device for LCD brightness control */
Jonathan Woithed0482532007-08-29 15:58:19 +0930346
Michał Kępieńf2252672017-03-20 10:32:19 +0100347static const struct key_entry keymap_backlight[] = {
348 { KE_KEY, true, { KEY_BRIGHTNESSUP } },
349 { KE_KEY, false, { KEY_BRIGHTNESSDOWN } },
350 { KE_END, 0 }
351};
352
Michał Kępień7d134e42017-03-20 10:32:17 +0100353static int acpi_fujitsu_bl_input_setup(struct acpi_device *device)
354{
355 struct fujitsu_bl *fujitsu_bl = acpi_driver_data(device);
Michał Kępieńf2252672017-03-20 10:32:19 +0100356 int ret;
Michał Kępień7d134e42017-03-20 10:32:17 +0100357
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100358 fujitsu_bl->input = devm_input_allocate_device(&device->dev);
359 if (!fujitsu_bl->input)
Michał Kępień7d134e42017-03-20 10:32:17 +0100360 return -ENOMEM;
361
362 snprintf(fujitsu_bl->phys, sizeof(fujitsu_bl->phys),
363 "%s/video/input0", acpi_device_hid(device));
364
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100365 fujitsu_bl->input->name = acpi_device_name(device);
366 fujitsu_bl->input->phys = fujitsu_bl->phys;
367 fujitsu_bl->input->id.bustype = BUS_HOST;
368 fujitsu_bl->input->id.product = 0x06;
Michał Kępieńf2252672017-03-20 10:32:19 +0100369
370 ret = sparse_keymap_setup(fujitsu_bl->input, keymap_backlight, NULL);
371 if (ret)
372 return ret;
Michał Kępień7d134e42017-03-20 10:32:17 +0100373
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100374 return input_register_device(fujitsu_bl->input);
Michał Kępień7d134e42017-03-20 10:32:17 +0100375}
376
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200377static int fujitsu_backlight_register(struct acpi_device *device)
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100378{
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200379 const struct backlight_properties props = {
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100380 .brightness = fujitsu_bl->brightness_level,
381 .max_brightness = fujitsu_bl->max_brightness - 1,
382 .type = BACKLIGHT_PLATFORM
383 };
384 struct backlight_device *bd;
385
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200386 bd = devm_backlight_device_register(&device->dev, "fujitsu-laptop",
387 &device->dev, NULL,
388 &fujitsu_bl_ops, &props);
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100389 if (IS_ERR(bd))
390 return PTR_ERR(bd);
391
392 fujitsu_bl->bl_device = bd;
393
394 return 0;
395}
396
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100397static int acpi_fujitsu_bl_add(struct acpi_device *device)
Jonathan Woithed0482532007-08-29 15:58:19 +0930398{
Jonathan Woithed0482532007-08-29 15:58:19 +0930399 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930400 int error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930401
Michał Kępień07acf622017-04-05 08:49:00 +0200402 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
403 return -ENODEV;
404
Jonathan Woithed0482532007-08-29 15:58:19 +0930405 if (!device)
406 return -EINVAL;
407
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100408 fujitsu_bl->acpi_handle = device->handle;
409 sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_BL_DEVICE_NAME);
Jonathan Woithed0482532007-08-29 15:58:19 +0930410 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100411 device->driver_data = fujitsu_bl;
Jonathan Woithed0482532007-08-29 15:58:19 +0930412
Michał Kępień7d134e42017-03-20 10:32:17 +0100413 error = acpi_fujitsu_bl_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930414 if (error)
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100415 return error;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930416
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100417 error = acpi_bus_update_power(fujitsu_bl->acpi_handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100418 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700419 pr_err("Error reading power state\n");
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100420 return error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930421 }
422
Joe Perches77bad7c2011-03-29 15:21:39 -0700423 pr_info("ACPI: %s [%s] (%s)\n",
Jonathan Woithed0482532007-08-29 15:58:19 +0930424 acpi_device_name(device), acpi_device_bid(device),
425 !device->power.state ? "on" : "off");
426
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800427 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930428 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
429 if (ACPI_FAILURE
430 (acpi_evaluate_object
431 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700432 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930433 }
434
Jonathan Woithe20b93732008-06-11 10:14:56 +0930435 if (get_max_brightness() <= 0)
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100436 fujitsu_bl->max_brightness = FUJITSU_LCD_N_LEVELS;
Tony Vroonf87a1a52009-01-07 10:11:24 +0000437 get_lcd_level();
Jonathan Woithe20b93732008-06-11 10:14:56 +0930438
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200439 error = fujitsu_backlight_register(device);
Michał Kępień07acf622017-04-05 08:49:00 +0200440 if (error)
441 return error;
Michał Kępieńaea31372017-03-10 11:50:35 +0100442
Julia Lawallb30bb892013-12-29 23:47:36 +0100443 return 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930444}
445
Jonathan Woithe20b93732008-06-11 10:14:56 +0930446/* Brightness notify */
447
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100448static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930449{
450 struct input_dev *input;
Michał Kępieńf2252672017-03-20 10:32:19 +0100451 int oldb, newb;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930452
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100453 input = fujitsu_bl->input;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930454
Michał Kępień5efc8002017-03-01 07:42:53 +0100455 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930456 vdbg_printk(FUJLAPTOP_DBG_WARN,
457 "unsupported event [0x%x]\n", event);
Michał Kępieńf2252672017-03-20 10:32:19 +0100458 sparse_keymap_report_event(input, -1, 1, true);
Michał Kępień5efc8002017-03-01 07:42:53 +0100459 return;
460 }
461
Michał Kępień5efc8002017-03-01 07:42:53 +0100462 oldb = fujitsu_bl->brightness_level;
463 get_lcd_level();
464 newb = fujitsu_bl->brightness_level;
465
Michał Kępień5c495d62017-04-05 08:49:10 +0200466 vdbg_printk(FUJLAPTOP_DBG_TRACE, "brightness button event [%i -> %i]\n",
467 oldb, newb);
Michał Kępień5efc8002017-03-01 07:42:53 +0100468
Michał Kępieńd2aa3ae2017-03-01 07:42:54 +0100469 if (oldb == newb)
470 return;
471
Michał Kępieńb4bb0cf2017-04-05 08:49:06 +0200472 if (!disable_brightness_adjust)
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200473 set_lcd_level(newb);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930474
Michał Kępieńf2252672017-03-20 10:32:19 +0100475 sparse_keymap_report_event(input, oldb < newb, 1, true);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930476}
477
478/* ACPI device for hotkey handling */
479
Michał Kępień527483a2017-03-20 10:32:22 +0100480static const struct key_entry keymap_default[] = {
481 { KE_KEY, KEY1_CODE, { KEY_PROG1 } },
482 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
483 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
484 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
485 { KE_KEY, KEY5_CODE, { KEY_RFKILL } },
486 { KE_KEY, BIT(26), { KEY_TOUCHPAD_TOGGLE } },
487 { KE_END, 0 }
488};
489
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100490static const struct key_entry keymap_s64x0[] = {
491 { KE_KEY, KEY1_CODE, { KEY_SCREENLOCK } }, /* "Lock" */
492 { KE_KEY, KEY2_CODE, { KEY_HELP } }, /* "Mobility Center */
493 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
494 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
495 { KE_END, 0 }
496};
497
498static const struct key_entry keymap_p8010[] = {
499 { KE_KEY, KEY1_CODE, { KEY_HELP } }, /* "Support" */
500 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
501 { KE_KEY, KEY3_CODE, { KEY_SWITCHVIDEOMODE } }, /* "Presentation" */
502 { KE_KEY, KEY4_CODE, { KEY_WWW } }, /* "WWW" */
503 { KE_END, 0 }
504};
505
Michał Kępień527483a2017-03-20 10:32:22 +0100506static const struct key_entry *keymap = keymap_default;
507
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100508static int fujitsu_laptop_dmi_keymap_override(const struct dmi_system_id *id)
509{
510 pr_info("Identified laptop model '%s'\n", id->ident);
511 keymap = id->driver_data;
512 return 1;
513}
514
515static const struct dmi_system_id fujitsu_laptop_dmi_table[] = {
516 {
517 .callback = fujitsu_laptop_dmi_keymap_override,
518 .ident = "Fujitsu Siemens S6410",
519 .matches = {
520 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
521 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
522 },
523 .driver_data = (void *)keymap_s64x0
524 },
525 {
526 .callback = fujitsu_laptop_dmi_keymap_override,
527 .ident = "Fujitsu Siemens S6420",
528 .matches = {
529 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
530 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6420"),
531 },
532 .driver_data = (void *)keymap_s64x0
533 },
534 {
535 .callback = fujitsu_laptop_dmi_keymap_override,
536 .ident = "Fujitsu LifeBook P8010",
537 .matches = {
538 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
539 DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
540 },
541 .driver_data = (void *)keymap_p8010
542 },
543 {}
544};
545
Michał Kępień11182db2017-03-20 10:32:20 +0100546static int acpi_fujitsu_laptop_input_setup(struct acpi_device *device)
547{
548 struct fujitsu_laptop *fujitsu_laptop = acpi_driver_data(device);
Michał Kępień527483a2017-03-20 10:32:22 +0100549 int ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100550
Michał Kępieńf66735f2017-03-20 10:32:21 +0100551 fujitsu_laptop->input = devm_input_allocate_device(&device->dev);
552 if (!fujitsu_laptop->input)
Michał Kępień11182db2017-03-20 10:32:20 +0100553 return -ENOMEM;
554
555 snprintf(fujitsu_laptop->phys, sizeof(fujitsu_laptop->phys),
556 "%s/video/input0", acpi_device_hid(device));
557
Michał Kępieńf66735f2017-03-20 10:32:21 +0100558 fujitsu_laptop->input->name = acpi_device_name(device);
559 fujitsu_laptop->input->phys = fujitsu_laptop->phys;
560 fujitsu_laptop->input->id.bustype = BUS_HOST;
561 fujitsu_laptop->input->id.product = 0x06;
Michał Kępień11182db2017-03-20 10:32:20 +0100562
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100563 dmi_check_system(fujitsu_laptop_dmi_table);
Michał Kępień527483a2017-03-20 10:32:22 +0100564 ret = sparse_keymap_setup(fujitsu_laptop->input, keymap, NULL);
565 if (ret)
566 return ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100567
Michał Kępieńf66735f2017-03-20 10:32:21 +0100568 return input_register_device(fujitsu_laptop->input);
Michał Kępień11182db2017-03-20 10:32:20 +0100569}
570
Michał Kępieńd811b512017-03-14 11:26:29 +0100571static int fujitsu_laptop_platform_add(void)
572{
573 int ret;
574
Michał Kępień979800e2017-03-14 11:26:31 +0100575 fujitsu_laptop->pf_device = platform_device_alloc("fujitsu-laptop", -1);
576 if (!fujitsu_laptop->pf_device)
Michał Kępieńd811b512017-03-14 11:26:29 +0100577 return -ENOMEM;
578
Michał Kępień979800e2017-03-14 11:26:31 +0100579 ret = platform_device_add(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100580 if (ret)
581 goto err_put_platform_device;
582
Michał Kępień979800e2017-03-14 11:26:31 +0100583 ret = sysfs_create_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100584 &fujitsu_pf_attribute_group);
585 if (ret)
586 goto err_del_platform_device;
587
588 return 0;
589
590err_del_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100591 platform_device_del(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100592err_put_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100593 platform_device_put(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100594
595 return ret;
596}
597
598static void fujitsu_laptop_platform_remove(void)
599{
Michał Kępień979800e2017-03-14 11:26:31 +0100600 sysfs_remove_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100601 &fujitsu_pf_attribute_group);
Michał Kępień979800e2017-03-14 11:26:31 +0100602 platform_device_unregister(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100603}
604
Michał Kępieńe33ca452017-04-07 15:07:10 +0200605static int logolamp_set(struct led_classdev *cdev,
606 enum led_brightness brightness)
607{
608 int poweron = FUNC_LED_ON, always = FUNC_LED_ON;
609 int ret;
610
611 if (brightness < LED_HALF)
612 poweron = FUNC_LED_OFF;
613
614 if (brightness < LED_FULL)
615 always = FUNC_LED_OFF;
616
617 ret = call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_POWERON, poweron);
618 if (ret < 0)
619 return ret;
620
621 return call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_ALWAYS, always);
622}
623
624static enum led_brightness logolamp_get(struct led_classdev *cdev)
625{
626 int ret;
627
628 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_ALWAYS, 0x0);
629 if (ret == FUNC_LED_ON)
630 return LED_FULL;
631
632 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_POWERON, 0x0);
633 if (ret == FUNC_LED_ON)
634 return LED_HALF;
635
636 return LED_OFF;
637}
638
639static struct led_classdev logolamp_led = {
640 .name = "fujitsu::logolamp",
641 .brightness_set_blocking = logolamp_set,
642 .brightness_get = logolamp_get
643};
644
645static int kblamps_set(struct led_classdev *cdev,
646 enum led_brightness brightness)
647{
648 if (brightness >= LED_FULL)
649 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
650 FUNC_LED_ON);
651 else
652 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
653 FUNC_LED_OFF);
654}
655
656static enum led_brightness kblamps_get(struct led_classdev *cdev)
657{
658 enum led_brightness brightness = LED_OFF;
659
660 if (call_fext_func(FUNC_LEDS, 0x2, KEYBOARD_LAMPS, 0x0) == FUNC_LED_ON)
661 brightness = LED_FULL;
662
663 return brightness;
664}
665
666static struct led_classdev kblamps_led = {
667 .name = "fujitsu::kblamps",
668 .brightness_set_blocking = kblamps_set,
669 .brightness_get = kblamps_get
670};
671
672static int radio_led_set(struct led_classdev *cdev,
673 enum led_brightness brightness)
674{
675 if (brightness >= LED_FULL)
676 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON,
677 RADIO_LED_ON);
678 else
679 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON, 0x0);
680}
681
682static enum led_brightness radio_led_get(struct led_classdev *cdev)
683{
684 enum led_brightness brightness = LED_OFF;
685
686 if (call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0) & RADIO_LED_ON)
687 brightness = LED_FULL;
688
689 return brightness;
690}
691
692static struct led_classdev radio_led = {
693 .name = "fujitsu::radio_led",
694 .brightness_set_blocking = radio_led_set,
695 .brightness_get = radio_led_get,
696 .default_trigger = "rfkill-any"
697};
698
699static int eco_led_set(struct led_classdev *cdev,
700 enum led_brightness brightness)
701{
702 int curr;
703
704 curr = call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0);
705 if (brightness >= LED_FULL)
706 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
707 curr | ECO_LED_ON);
708 else
709 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
710 curr & ~ECO_LED_ON);
711}
712
713static enum led_brightness eco_led_get(struct led_classdev *cdev)
714{
715 enum led_brightness brightness = LED_OFF;
716
717 if (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) & ECO_LED_ON)
718 brightness = LED_FULL;
719
720 return brightness;
721}
722
723static struct led_classdev eco_led = {
724 .name = "fujitsu::eco_led",
725 .brightness_set_blocking = eco_led_set,
726 .brightness_get = eco_led_get
727};
728
Michał Kępień7adb7b12017-04-07 15:07:09 +0200729static int acpi_fujitsu_laptop_leds_register(void)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930730{
Jonathan Woithe20b93732008-06-11 10:14:56 +0930731 int result = 0;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200732
733 if (call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
734 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
735 &logolamp_led);
736 if (result == 0) {
737 fujitsu_laptop->logolamp_registered = 1;
738 } else {
739 pr_err("Could not register LED handler for logo lamp, error %i\n",
740 result);
741 }
742 }
743
744 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
745 (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
746 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
747 &kblamps_led);
748 if (result == 0) {
749 fujitsu_laptop->kblamps_registered = 1;
750 } else {
751 pr_err("Could not register LED handler for keyboard lamps, error %i\n",
752 result);
753 }
754 }
755
756 /*
757 * BTNI bit 24 seems to indicate the presence of a radio toggle
758 * button in place of a slide switch, and all such machines appear
759 * to also have an RF LED. Therefore use bit 24 as an indicator
760 * that an RF LED is present.
761 */
762 if (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) & BIT(24)) {
763 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
764 &radio_led);
765 if (result == 0) {
766 fujitsu_laptop->radio_led_registered = 1;
767 } else {
768 pr_err("Could not register LED handler for radio LED, error %i\n",
769 result);
770 }
771 }
772
773 /* Support for eco led is not always signaled in bit corresponding
774 * to the bit used to control the led. According to the DSDT table,
775 * bit 14 seems to indicate presence of said led as well.
776 * Confirm by testing the status.
777 */
778 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
779 (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
780 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
781 &eco_led);
782 if (result == 0) {
783 fujitsu_laptop->eco_led_registered = 1;
784 } else {
785 pr_err("Could not register LED handler for eco LED, error %i\n",
786 result);
787 }
788 }
789
790 return result;
791}
792
793static int acpi_fujitsu_laptop_add(struct acpi_device *device)
794{
Jonathan Woithe20b93732008-06-11 10:14:56 +0930795 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930796 int error;
797 int i;
798
799 if (!device)
800 return -EINVAL;
801
Alan Jenkins6942eab2017-02-08 14:46:25 +0100802 fujitsu_laptop->acpi_handle = device->handle;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930803 sprintf(acpi_device_name(device), "%s",
Alan Jenkins6942eab2017-02-08 14:46:25 +0100804 ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930805 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Alan Jenkins6942eab2017-02-08 14:46:25 +0100806 device->driver_data = fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930807
Jonathan Woithe20b93732008-06-11 10:14:56 +0930808 /* kfifo */
Alan Jenkins6942eab2017-02-08 14:46:25 +0100809 spin_lock_init(&fujitsu_laptop->fifo_lock);
810 error = kfifo_alloc(&fujitsu_laptop->fifo, RINGBUFFERSIZE * sizeof(int),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800811 GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800812 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700813 pr_err("kfifo_alloc failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930814 goto err_stop;
815 }
816
Michał Kępień11182db2017-03-20 10:32:20 +0100817 error = acpi_fujitsu_laptop_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930818 if (error)
Michał Kępień11182db2017-03-20 10:32:20 +0100819 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930820
Alan Jenkins6942eab2017-02-08 14:46:25 +0100821 error = acpi_bus_update_power(fujitsu_laptop->acpi_handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100822 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700823 pr_err("Error reading power state\n");
Michał Kępieńf66735f2017-03-20 10:32:21 +0100824 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930825 }
826
Joe Perches77bad7c2011-03-29 15:21:39 -0700827 pr_info("ACPI: %s [%s] (%s)\n",
828 acpi_device_name(device), acpi_device_bid(device),
829 !device->power.state ? "on" : "off");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930830
Alan Jenkins6942eab2017-02-08 14:46:25 +0100831 fujitsu_laptop->dev = device;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930832
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800833 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930834 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
835 if (ACPI_FAILURE
836 (acpi_evaluate_object
837 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700838 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930839 }
840
Tony Vroon3a407082008-12-31 18:19:59 +0000841 i = 0;
842 while (call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0
843 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE)
844 ; /* No action, result is discarded */
Jonathan Woithe20b93732008-06-11 10:14:56 +0930845 vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
846
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100847 fujitsu_laptop->flags_supported =
848 call_fext_func(FUNC_FLAGS, 0x0, 0x0, 0x0);
Tony Vroon4898c2b2009-02-02 11:11:10 +0000849
850 /* Make sure our bitmask of supported functions is cleared if the
851 RFKILL function block is not implemented, like on the S7020. */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100852 if (fujitsu_laptop->flags_supported == UNSUPPORTED_CMD)
853 fujitsu_laptop->flags_supported = 0;
Tony Vroon4898c2b2009-02-02 11:11:10 +0000854
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100855 if (fujitsu_laptop->flags_supported)
856 fujitsu_laptop->flags_state =
857 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
Tony Vroon3a407082008-12-31 18:19:59 +0000858
859 /* Suspect this is a keymap of the application panel, print it */
Joe Perches77bad7c2011-03-29 15:21:39 -0700860 pr_info("BTNI: [0x%x]\n", call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0));
Tony Vroon3a407082008-12-31 18:19:59 +0000861
Michał Kępień1877e262017-03-10 11:50:34 +0100862 /* Sync backlight power status */
Michał Kępieńaea31372017-03-10 11:50:35 +0100863 if (fujitsu_bl->bl_device &&
864 acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Michał Kępień1877e262017-03-10 11:50:34 +0100865 if (call_fext_func(FUNC_BACKLIGHT, 0x2, 0x4, 0x0) == 3)
866 fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
867 else
868 fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
869 }
870
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100871 error = fujitsu_laptop_platform_add();
872 if (error)
Michał Kępieńf66735f2017-03-20 10:32:21 +0100873 goto err_free_fifo;
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100874
Michał Kępień7adb7b12017-04-07 15:07:09 +0200875 error = acpi_fujitsu_laptop_leds_register();
876 if (error)
877 goto err_remove_platform_device;
Tony Vroon3a407082008-12-31 18:19:59 +0000878
Michał Kępień7adb7b12017-04-07 15:07:09 +0200879 return 0;
Michał Kępień4f625682016-04-12 22:06:34 +0930880
Michał Kępień7adb7b12017-04-07 15:07:09 +0200881err_remove_platform_device:
882 fujitsu_laptop_platform_remove();
Bjorn Helgaasb4ec0272009-04-07 15:37:22 +0000883err_free_fifo:
Alan Jenkins6942eab2017-02-08 14:46:25 +0100884 kfifo_free(&fujitsu_laptop->fifo);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930885err_stop:
Julia Lawallb30bb892013-12-29 23:47:36 +0100886 return error;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930887}
888
Alan Jenkins6942eab2017-02-08 14:46:25 +0100889static int acpi_fujitsu_laptop_remove(struct acpi_device *device)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930890{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100891 struct fujitsu_laptop *fujitsu_laptop = acpi_driver_data(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930892
Alan Jenkins6942eab2017-02-08 14:46:25 +0100893 if (fujitsu_laptop->logolamp_registered)
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +0930894 led_classdev_unregister(&logolamp_led);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930895
Alan Jenkins6942eab2017-02-08 14:46:25 +0100896 if (fujitsu_laptop->kblamps_registered)
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +0930897 led_classdev_unregister(&kblamps_led);
Michał Kępień4f625682016-04-12 22:06:34 +0930898
Alan Jenkins6942eab2017-02-08 14:46:25 +0100899 if (fujitsu_laptop->radio_led_registered)
Michał Kępień4f625682016-04-12 22:06:34 +0930900 led_classdev_unregister(&radio_led);
Matej Gromad6b88f62016-06-21 10:09:21 +0200901
Alan Jenkins6942eab2017-02-08 14:46:25 +0100902 if (fujitsu_laptop->eco_led_registered)
Matej Gromad6b88f62016-06-21 10:09:21 +0200903 led_classdev_unregister(&eco_led);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930904
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100905 fujitsu_laptop_platform_remove();
906
Alan Jenkins6942eab2017-02-08 14:46:25 +0100907 kfifo_free(&fujitsu_laptop->fifo);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930908
Alan Jenkins6942eab2017-02-08 14:46:25 +0100909 fujitsu_laptop->acpi_handle = NULL;
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +0930910
Jonathan Woithe20b93732008-06-11 10:14:56 +0930911 return 0;
912}
913
Michał Kępień527483a2017-03-20 10:32:22 +0100914static void acpi_fujitsu_laptop_press(int scancode)
Michał Kępień2451d192017-01-11 09:59:31 +0100915{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100916 struct input_dev *input = fujitsu_laptop->input;
Michał Kępień2451d192017-01-11 09:59:31 +0100917 int status;
918
Alan Jenkins6942eab2017-02-08 14:46:25 +0100919 status = kfifo_in_locked(&fujitsu_laptop->fifo,
Michał Kępień527483a2017-03-20 10:32:22 +0100920 (unsigned char *)&scancode, sizeof(scancode),
Alan Jenkins6942eab2017-02-08 14:46:25 +0100921 &fujitsu_laptop->fifo_lock);
Michał Kępień527483a2017-03-20 10:32:22 +0100922 if (status != sizeof(scancode)) {
Michał Kępień2451d192017-01-11 09:59:31 +0100923 vdbg_printk(FUJLAPTOP_DBG_WARN,
Michał Kępień527483a2017-03-20 10:32:22 +0100924 "Could not push scancode [0x%x]\n", scancode);
Michał Kępieńa28c7e92017-01-11 09:59:33 +0100925 return;
Michał Kępień2451d192017-01-11 09:59:31 +0100926 }
Michał Kępień527483a2017-03-20 10:32:22 +0100927 sparse_keymap_report_event(input, scancode, 1, false);
Michał Kępieńa28c7e92017-01-11 09:59:33 +0100928 vdbg_printk(FUJLAPTOP_DBG_TRACE,
Michał Kępień527483a2017-03-20 10:32:22 +0100929 "Push scancode into ringbuffer [0x%x]\n", scancode);
Michał Kępień2451d192017-01-11 09:59:31 +0100930}
931
Alan Jenkins6942eab2017-02-08 14:46:25 +0100932static void acpi_fujitsu_laptop_release(void)
Michał Kępień2451d192017-01-11 09:59:31 +0100933{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100934 struct input_dev *input = fujitsu_laptop->input;
Michał Kępień527483a2017-03-20 10:32:22 +0100935 int scancode, status;
Michał Kępień2451d192017-01-11 09:59:31 +0100936
Michał Kępień29544f02017-01-11 09:59:32 +0100937 while (true) {
Alan Jenkins6942eab2017-02-08 14:46:25 +0100938 status = kfifo_out_locked(&fujitsu_laptop->fifo,
Michał Kępień527483a2017-03-20 10:32:22 +0100939 (unsigned char *)&scancode,
940 sizeof(scancode),
Alan Jenkins6942eab2017-02-08 14:46:25 +0100941 &fujitsu_laptop->fifo_lock);
Michał Kępień527483a2017-03-20 10:32:22 +0100942 if (status != sizeof(scancode))
Michał Kępień29544f02017-01-11 09:59:32 +0100943 return;
Michał Kępień527483a2017-03-20 10:32:22 +0100944 sparse_keymap_report_event(input, scancode, 0, false);
Michał Kępień2451d192017-01-11 09:59:31 +0100945 vdbg_printk(FUJLAPTOP_DBG_TRACE,
Michał Kępień527483a2017-03-20 10:32:22 +0100946 "Pop scancode from ringbuffer [0x%x]\n", scancode);
Michał Kępień2451d192017-01-11 09:59:31 +0100947 }
948}
949
Alan Jenkins6942eab2017-02-08 14:46:25 +0100950static void acpi_fujitsu_laptop_notify(struct acpi_device *device, u32 event)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930951{
952 struct input_dev *input;
Michał Kępień527483a2017-03-20 10:32:22 +0100953 int scancode, i = 0;
954 unsigned int irb;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930955
Alan Jenkins6942eab2017-02-08 14:46:25 +0100956 input = fujitsu_laptop->input;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930957
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100958 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930959 vdbg_printk(FUJLAPTOP_DBG_WARN,
960 "Unsupported event [0x%x]\n", event);
Michał Kępień527483a2017-03-20 10:32:22 +0100961 sparse_keymap_report_event(input, -1, 1, true);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100962 return;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930963 }
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100964
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100965 if (fujitsu_laptop->flags_supported)
966 fujitsu_laptop->flags_state =
967 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100968
Michał Kępień527483a2017-03-20 10:32:22 +0100969 while ((irb = call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0)) != 0 &&
970 i++ < MAX_HOTKEY_RINGBUFFER_SIZE) {
971 scancode = irb & 0x4ff;
972 if (sparse_keymap_entry_from_scancode(input, scancode))
973 acpi_fujitsu_laptop_press(scancode);
974 else if (scancode == 0)
975 acpi_fujitsu_laptop_release();
976 else
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100977 vdbg_printk(FUJLAPTOP_DBG_WARN,
978 "Unknown GIRB result [%x]\n", irb);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100979 }
980
981 /* On some models (first seen on the Skylake-based Lifebook
982 * E736/E746/E756), the touchpad toggle hotkey (Fn+F4) is
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100983 * handled in software; its state is queried using FUNC_FLAGS
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100984 */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100985 if ((fujitsu_laptop->flags_supported & BIT(26)) &&
Michał Kępień527483a2017-03-20 10:32:22 +0100986 (call_fext_func(FUNC_FLAGS, 0x1, 0x0, 0x0) & BIT(26)))
987 sparse_keymap_report_event(input, BIT(26), 1, true);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930988}
989
990/* Initialization */
991
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100992static const struct acpi_device_id fujitsu_bl_device_ids[] = {
993 {ACPI_FUJITSU_BL_HID, 0},
Jonathan Woithed0482532007-08-29 15:58:19 +0930994 {"", 0},
995};
996
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100997static struct acpi_driver acpi_fujitsu_bl_driver = {
998 .name = ACPI_FUJITSU_BL_DRIVER_NAME,
Jonathan Woithed0482532007-08-29 15:58:19 +0930999 .class = ACPI_FUJITSU_CLASS,
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001000 .ids = fujitsu_bl_device_ids,
Jonathan Woithed0482532007-08-29 15:58:19 +09301001 .ops = {
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001002 .add = acpi_fujitsu_bl_add,
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001003 .notify = acpi_fujitsu_bl_notify,
Jonathan Woithed0482532007-08-29 15:58:19 +09301004 },
1005};
1006
Alan Jenkins6942eab2017-02-08 14:46:25 +01001007static const struct acpi_device_id fujitsu_laptop_device_ids[] = {
1008 {ACPI_FUJITSU_LAPTOP_HID, 0},
Jonathan Woithe20b93732008-06-11 10:14:56 +09301009 {"", 0},
1010};
1011
Alan Jenkins6942eab2017-02-08 14:46:25 +01001012static struct acpi_driver acpi_fujitsu_laptop_driver = {
1013 .name = ACPI_FUJITSU_LAPTOP_DRIVER_NAME,
Jonathan Woithe20b93732008-06-11 10:14:56 +09301014 .class = ACPI_FUJITSU_CLASS,
Alan Jenkins6942eab2017-02-08 14:46:25 +01001015 .ids = fujitsu_laptop_device_ids,
Jonathan Woithe20b93732008-06-11 10:14:56 +09301016 .ops = {
Alan Jenkins6942eab2017-02-08 14:46:25 +01001017 .add = acpi_fujitsu_laptop_add,
1018 .remove = acpi_fujitsu_laptop_remove,
1019 .notify = acpi_fujitsu_laptop_notify,
Jonathan Woithe20b93732008-06-11 10:14:56 +09301020 },
1021};
Jonathan Woithed0482532007-08-29 15:58:19 +09301022
Zhang Rui49901412014-09-09 00:21:59 +02001023static const struct acpi_device_id fujitsu_ids[] __used = {
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001024 {ACPI_FUJITSU_BL_HID, 0},
Alan Jenkins6942eab2017-02-08 14:46:25 +01001025 {ACPI_FUJITSU_LAPTOP_HID, 0},
Zhang Rui49901412014-09-09 00:21:59 +02001026 {"", 0}
1027};
1028MODULE_DEVICE_TABLE(acpi, fujitsu_ids);
1029
Jonathan Woithed0482532007-08-29 15:58:19 +09301030static int __init fujitsu_init(void)
1031{
Michał Kępieńb8d69c12017-03-10 11:50:33 +01001032 int ret;
Jonathan Woithed0482532007-08-29 15:58:19 +09301033
1034 if (acpi_disabled)
1035 return -ENODEV;
1036
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001037 fujitsu_bl = kzalloc(sizeof(struct fujitsu_bl), GFP_KERNEL);
1038 if (!fujitsu_bl)
Jonathan Woithed0482532007-08-29 15:58:19 +09301039 return -ENOMEM;
Jonathan Woithed0482532007-08-29 15:58:19 +09301040
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001041 ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
1042 if (ret)
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001043 goto err_free_fujitsu_bl;
Jonathan Woithed0482532007-08-29 15:58:19 +09301044
Jonathan Woithed0482532007-08-29 15:58:19 +09301045 /* Register platform stuff */
1046
Alan Jenkins16506022017-02-08 14:46:26 +01001047 ret = platform_driver_register(&fujitsu_pf_driver);
Jonathan Woithe20b93732008-06-11 10:14:56 +09301048 if (ret)
Michał Kępieńc33f4c02017-03-14 11:26:30 +01001049 goto err_unregister_acpi;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301050
Alan Jenkins6942eab2017-02-08 14:46:25 +01001051 /* Register laptop driver */
Jonathan Woithe20b93732008-06-11 10:14:56 +09301052
Alan Jenkins6942eab2017-02-08 14:46:25 +01001053 fujitsu_laptop = kzalloc(sizeof(struct fujitsu_laptop), GFP_KERNEL);
1054 if (!fujitsu_laptop) {
Jonathan Woithe20b93732008-06-11 10:14:56 +09301055 ret = -ENOMEM;
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001056 goto err_unregister_platform_driver;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301057 }
Jonathan Woithe20b93732008-06-11 10:14:56 +09301058
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001059 ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
1060 if (ret)
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001061 goto err_free_fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301062
Joe Perches77bad7c2011-03-29 15:21:39 -07001063 pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301064
1065 return 0;
1066
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001067err_free_fujitsu_laptop:
Alan Jenkins6942eab2017-02-08 14:46:25 +01001068 kfree(fujitsu_laptop);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001069err_unregister_platform_driver:
Alan Jenkins16506022017-02-08 14:46:26 +01001070 platform_driver_unregister(&fujitsu_pf_driver);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001071err_unregister_acpi:
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001072 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001073err_free_fujitsu_bl:
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001074 kfree(fujitsu_bl);
Jonathan Woithed0482532007-08-29 15:58:19 +09301075
1076 return ret;
1077}
1078
1079static void __exit fujitsu_cleanup(void)
1080{
Alan Jenkins6942eab2017-02-08 14:46:25 +01001081 acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
Tony Vroon3a407082008-12-31 18:19:59 +00001082
Alan Jenkins6942eab2017-02-08 14:46:25 +01001083 kfree(fujitsu_laptop);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301084
Alan Jenkins16506022017-02-08 14:46:26 +01001085 platform_driver_unregister(&fujitsu_pf_driver);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301086
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001087 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Jonathan Woithed0482532007-08-29 15:58:19 +09301088
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001089 kfree(fujitsu_bl);
Jonathan Woithed0482532007-08-29 15:58:19 +09301090
Joe Perches77bad7c2011-03-29 15:21:39 -07001091 pr_info("driver unloaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301092}
1093
1094module_init(fujitsu_init);
1095module_exit(fujitsu_cleanup);
1096
Michał Kępieńe06e4832017-04-05 08:49:05 +02001097module_param(use_alt_lcd_levels, int, 0644);
1098MODULE_PARM_DESC(use_alt_lcd_levels, "Interface used for setting LCD brightness level (-1 = auto, 0 = force SBLL, 1 = force SBL2)");
Michał Kępieńb4bb0cf2017-04-05 08:49:06 +02001099module_param(disable_brightness_adjust, bool, 0644);
1100MODULE_PARM_DESC(disable_brightness_adjust, "Disable LCD brightness adjustment");
Jonathan Woithe20b93732008-06-11 10:14:56 +09301101#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
1102module_param_named(debug, dbg_level, uint, 0644);
1103MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
1104#endif
1105
Tony Vroon3a407082008-12-31 18:19:59 +00001106MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
Jonathan Woithed0482532007-08-29 15:58:19 +09301107MODULE_DESCRIPTION("Fujitsu laptop extras support");
1108MODULE_VERSION(FUJITSU_DRIVER_VERSION);
1109MODULE_LICENSE("GPL");