blob: 1124070aad2de4079415d51293687115f07f1c57 [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 Woithe20b93732008-06-11 10:14:56 +0930133 struct input_dev *input;
134 char phys[32];
Jonathan Woithed0482532007-08-29 15:58:19 +0930135 struct backlight_device *bl_device;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930136 unsigned int max_brightness;
Jonathan Woithed0482532007-08-29 15:58:19 +0930137 unsigned int brightness_level;
138};
139
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100140static struct fujitsu_bl *fujitsu_bl;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930141static int use_alt_lcd_levels = -1;
Michał Kępieńb4bb0cf2017-04-05 08:49:06 +0200142static bool disable_brightness_adjust;
Jonathan Woithed0482532007-08-29 15:58:19 +0930143
Alan Jenkins6942eab2017-02-08 14:46:25 +0100144/* Device used to access hotkeys and other features on the laptop */
145struct fujitsu_laptop {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930146 acpi_handle acpi_handle;
147 struct acpi_device *dev;
148 struct input_dev *input;
149 char phys[32];
150 struct platform_device *pf_device;
Stefani Seibold45465482009-12-21 14:37:26 -0800151 struct kfifo fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930152 spinlock_t fifo_lock;
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100153 int flags_supported;
154 int flags_state;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930155};
156
Alan Jenkins6942eab2017-02-08 14:46:25 +0100157static struct fujitsu_laptop *fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930158
Jonathan Woithe20b93732008-06-11 10:14:56 +0930159#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
160static u32 dbg_level = 0x03;
161#endif
162
Tony Vroon3a407082008-12-31 18:19:59 +0000163/* Fujitsu ACPI interface function */
164
Michał Kępieńf68e4922017-04-03 11:38:59 +0200165static int call_fext_func(int func, int op, int feature, int state)
Tony Vroon3a407082008-12-31 18:19:59 +0000166{
Tony Vroon3a407082008-12-31 18:19:59 +0000167 union acpi_object params[4] = {
Michał Kępieńf68e4922017-04-03 11:38:59 +0200168 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = func },
169 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = op },
170 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = feature },
171 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = state }
Tony Vroon3a407082008-12-31 18:19:59 +0000172 };
Michał Kępieńb1066412017-04-03 11:38:57 +0200173 struct acpi_object_list arg_list = { 4, params };
Zhang Rui29c29a92013-09-03 08:32:12 +0800174 unsigned long long value;
Michał Kępieńb1066412017-04-03 11:38:57 +0200175 acpi_status status;
Tony Vroon3a407082008-12-31 18:19:59 +0000176
Michał Kępień17e23552017-04-03 11:38:58 +0200177 status = acpi_evaluate_integer(fujitsu_laptop->acpi_handle, "FUNC",
178 &arg_list, &value);
Tony Vroon3a407082008-12-31 18:19:59 +0000179 if (ACPI_FAILURE(status)) {
Michał Kępień09b29e12017-04-06 08:46:10 +0200180 vdbg_printk(FUJLAPTOP_DBG_ERROR, "Failed to evaluate FUNC\n");
Tony Vroon3a407082008-12-31 18:19:59 +0000181 return -ENODEV;
182 }
183
Michał Kępieńf68e4922017-04-03 11:38:59 +0200184 vdbg_printk(FUJLAPTOP_DBG_TRACE, "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) returned 0x%x\n",
185 func, op, feature, state, (int)value);
Zhang Rui29c29a92013-09-03 08:32:12 +0800186 return value;
Tony Vroon3a407082008-12-31 18:19:59 +0000187}
188
Jonathan Woithe20b93732008-06-11 10:14:56 +0930189/* Hardware access for LCD brightness control */
Jonathan Woithed0482532007-08-29 15:58:19 +0930190
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200191static int set_lcd_level(struct acpi_device *device, int level)
Jonathan Woithed0482532007-08-29 15:58:19 +0930192{
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200193 struct fujitsu_bl *priv = acpi_driver_data(device);
Michał Kępieńa8779c32017-04-05 08:49:03 +0200194 acpi_status status;
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200195 char *method;
Jonathan Woithed0482532007-08-29 15:58:19 +0930196
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200197 switch (use_alt_lcd_levels) {
Michał Kępieńe06e4832017-04-05 08:49:05 +0200198 case -1:
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200199 if (acpi_has_method(device->handle, "SBL2"))
Michał Kępieńe06e4832017-04-05 08:49:05 +0200200 method = "SBL2";
201 else
202 method = "SBLL";
203 break;
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200204 case 1:
205 method = "SBL2";
206 break;
207 default:
208 method = "SBLL";
209 break;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930210 }
211
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200212 vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via %s [%d]\n",
213 method, level);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930214
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200215 if (level < 0 || level >= priv->max_brightness)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930216 return -EINVAL;
217
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200218 status = acpi_execute_simple_method(device->handle, method, level);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930219 if (ACPI_FAILURE(status)) {
Michał Kępieńa8779c32017-04-05 08:49:03 +0200220 vdbg_printk(FUJLAPTOP_DBG_ERROR, "Failed to evaluate %s\n",
221 method);
Jonathan Woithed0482532007-08-29 15:58:19 +0930222 return -ENODEV;
223 }
224
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200225 priv->brightness_level = level;
Michał Kępieńbd079a22017-04-05 08:49:04 +0200226
Jonathan Woithed0482532007-08-29 15:58:19 +0930227 return 0;
228}
229
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200230static int get_lcd_level(struct acpi_device *device)
Jonathan Woithed0482532007-08-29 15:58:19 +0930231{
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200232 struct fujitsu_bl *priv = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400233 unsigned long long state = 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930234 acpi_status status = AE_OK;
235
Jonathan Woithe20b93732008-06-11 10:14:56 +0930236 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLL\n");
237
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200238 status = acpi_evaluate_integer(device->handle, "GBLL", NULL, &state);
Jonathan Woithe3b1c37c2009-12-23 09:19:42 +1030239 if (ACPI_FAILURE(status))
240 return 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930241
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200242 priv->brightness_level = state & 0x0fffffff;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930243
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200244 return priv->brightness_level;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930245}
246
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200247static int get_max_brightness(struct acpi_device *device)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930248{
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200249 struct fujitsu_bl *priv = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400250 unsigned long long state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930251 acpi_status status = AE_OK;
252
253 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get max lcd level via RBLL\n");
254
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200255 status = acpi_evaluate_integer(device->handle, "RBLL", NULL, &state);
Jonathan Woithe3b1c37c2009-12-23 09:19:42 +1030256 if (ACPI_FAILURE(status))
257 return -1;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930258
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200259 priv->max_brightness = state;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930260
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200261 return priv->max_brightness;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930262}
263
Jonathan Woithed0482532007-08-29 15:58:19 +0930264/* Backlight device stuff */
265
266static int bl_get_brightness(struct backlight_device *b)
267{
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200268 struct acpi_device *device = bl_get_data(b);
269
270 return b->props.power == FB_BLANK_POWERDOWN ? 0 : get_lcd_level(device);
Jonathan Woithed0482532007-08-29 15:58:19 +0930271}
272
273static int bl_update_status(struct backlight_device *b)
274{
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200275 struct acpi_device *device = bl_get_data(b);
276
Michael Karchere8549e22015-01-18 20:28:46 +0100277 if (b->props.power == FB_BLANK_POWERDOWN)
Michał Kępieńf7c4c3f2017-04-05 08:49:07 +0200278 call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x3);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930279 else
Michał Kępieńf7c4c3f2017-04-05 08:49:07 +0200280 call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x0);
Tony Vroon3a407082008-12-31 18:19:59 +0000281
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200282 return set_lcd_level(device, b->props.brightness);
Jonathan Woithed0482532007-08-29 15:58:19 +0930283}
284
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100285static const struct backlight_ops fujitsu_bl_ops = {
Jonathan Woithed0482532007-08-29 15:58:19 +0930286 .get_brightness = bl_get_brightness,
287 .update_status = bl_update_status,
288};
289
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100290static ssize_t lid_show(struct device *dev, struct device_attribute *attr,
291 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000292{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100293 if (!(fujitsu_laptop->flags_supported & FLAG_LID))
Tony Vroon3a407082008-12-31 18:19:59 +0000294 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100295 if (fujitsu_laptop->flags_state & FLAG_LID)
Tony Vroon3a407082008-12-31 18:19:59 +0000296 return sprintf(buf, "open\n");
297 else
298 return sprintf(buf, "closed\n");
299}
300
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100301static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
302 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000303{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100304 if (!(fujitsu_laptop->flags_supported & FLAG_DOCK))
Tony Vroon3a407082008-12-31 18:19:59 +0000305 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100306 if (fujitsu_laptop->flags_state & FLAG_DOCK)
Tony Vroon3a407082008-12-31 18:19:59 +0000307 return sprintf(buf, "docked\n");
308 else
309 return sprintf(buf, "undocked\n");
310}
311
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100312static ssize_t radios_show(struct device *dev, struct device_attribute *attr,
313 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000314{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100315 if (!(fujitsu_laptop->flags_supported & FLAG_RFKILL))
Tony Vroon3a407082008-12-31 18:19:59 +0000316 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100317 if (fujitsu_laptop->flags_state & FLAG_RFKILL)
Tony Vroon3a407082008-12-31 18:19:59 +0000318 return sprintf(buf, "on\n");
319 else
320 return sprintf(buf, "killed\n");
321}
322
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100323static DEVICE_ATTR_RO(lid);
324static DEVICE_ATTR_RO(dock);
325static DEVICE_ATTR_RO(radios);
Jonathan Woithed0482532007-08-29 15:58:19 +0930326
Alan Jenkins16506022017-02-08 14:46:26 +0100327static struct attribute *fujitsu_pf_attributes[] = {
Tony Vroon3a407082008-12-31 18:19:59 +0000328 &dev_attr_lid.attr,
329 &dev_attr_dock.attr,
330 &dev_attr_radios.attr,
Jonathan Woithed0482532007-08-29 15:58:19 +0930331 NULL
332};
333
Alan Jenkins16506022017-02-08 14:46:26 +0100334static struct attribute_group fujitsu_pf_attribute_group = {
335 .attrs = fujitsu_pf_attributes
Jonathan Woithed0482532007-08-29 15:58:19 +0930336};
337
Alan Jenkins16506022017-02-08 14:46:26 +0100338static struct platform_driver fujitsu_pf_driver = {
Jonathan Woithed0482532007-08-29 15:58:19 +0930339 .driver = {
340 .name = "fujitsu-laptop",
Jonathan Woithed0482532007-08-29 15:58:19 +0930341 }
342};
343
Jonathan Woithe20b93732008-06-11 10:14:56 +0930344/* ACPI device for LCD brightness control */
Jonathan Woithed0482532007-08-29 15:58:19 +0930345
Michał Kępieńf2252672017-03-20 10:32:19 +0100346static const struct key_entry keymap_backlight[] = {
347 { KE_KEY, true, { KEY_BRIGHTNESSUP } },
348 { KE_KEY, false, { KEY_BRIGHTNESSDOWN } },
349 { KE_END, 0 }
350};
351
Michał Kępień7d134e42017-03-20 10:32:17 +0100352static int acpi_fujitsu_bl_input_setup(struct acpi_device *device)
353{
Michał Kępień7ec3b542017-05-19 09:44:41 +0200354 struct fujitsu_bl *priv = acpi_driver_data(device);
Michał Kępieńf2252672017-03-20 10:32:19 +0100355 int ret;
Michał Kępień7d134e42017-03-20 10:32:17 +0100356
Michał Kępień7ec3b542017-05-19 09:44:41 +0200357 priv->input = devm_input_allocate_device(&device->dev);
358 if (!priv->input)
Michał Kępień7d134e42017-03-20 10:32:17 +0100359 return -ENOMEM;
360
Michał Kępień7ec3b542017-05-19 09:44:41 +0200361 snprintf(priv->phys, sizeof(priv->phys), "%s/video/input0",
362 acpi_device_hid(device));
Michał Kępień7d134e42017-03-20 10:32:17 +0100363
Michał Kępień7ec3b542017-05-19 09:44:41 +0200364 priv->input->name = acpi_device_name(device);
365 priv->input->phys = priv->phys;
366 priv->input->id.bustype = BUS_HOST;
367 priv->input->id.product = 0x06;
Michał Kępieńf2252672017-03-20 10:32:19 +0100368
Michał Kępień7ec3b542017-05-19 09:44:41 +0200369 ret = sparse_keymap_setup(priv->input, keymap_backlight, NULL);
Michał Kępieńf2252672017-03-20 10:32:19 +0100370 if (ret)
371 return ret;
Michał Kępień7d134e42017-03-20 10:32:17 +0100372
Michał Kępień7ec3b542017-05-19 09:44:41 +0200373 return input_register_device(priv->input);
Michał Kępień7d134e42017-03-20 10:32:17 +0100374}
375
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200376static int fujitsu_backlight_register(struct acpi_device *device)
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100377{
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200378 struct fujitsu_bl *priv = acpi_driver_data(device);
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200379 const struct backlight_properties props = {
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200380 .brightness = priv->brightness_level,
381 .max_brightness = priv->max_brightness - 1,
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100382 .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",
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200387 &device->dev, device,
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200388 &fujitsu_bl_ops, &props);
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100389 if (IS_ERR(bd))
390 return PTR_ERR(bd);
391
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200392 priv->bl_device = bd;
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100393
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{
Michał Kępień679374e2017-05-19 09:44:42 +0200399 struct fujitsu_bl *priv;
Jonathan Woithed0482532007-08-29 15:58:19 +0930400 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930401 int error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930402
Michał Kępień07acf622017-04-05 08:49:00 +0200403 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
404 return -ENODEV;
405
Jonathan Woithed0482532007-08-29 15:58:19 +0930406 if (!device)
407 return -EINVAL;
408
Michał Kępień679374e2017-05-19 09:44:42 +0200409 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
410 if (!priv)
411 return -ENOMEM;
412
413 fujitsu_bl = priv;
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100414 sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_BL_DEVICE_NAME);
Jonathan Woithed0482532007-08-29 15:58:19 +0930415 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Michał Kępień679374e2017-05-19 09:44:42 +0200416 device->driver_data = priv;
Jonathan Woithed0482532007-08-29 15:58:19 +0930417
Michał Kępień7d134e42017-03-20 10:32:17 +0100418 error = acpi_fujitsu_bl_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930419 if (error)
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100420 return error;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930421
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200422 error = acpi_bus_update_power(device->handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100423 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700424 pr_err("Error reading power state\n");
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100425 return error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930426 }
427
Joe Perches77bad7c2011-03-29 15:21:39 -0700428 pr_info("ACPI: %s [%s] (%s)\n",
Jonathan Woithed0482532007-08-29 15:58:19 +0930429 acpi_device_name(device), acpi_device_bid(device),
430 !device->power.state ? "on" : "off");
431
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800432 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930433 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
434 if (ACPI_FAILURE
435 (acpi_evaluate_object
436 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700437 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930438 }
439
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200440 if (get_max_brightness(device) <= 0)
441 priv->max_brightness = FUJITSU_LCD_N_LEVELS;
442 get_lcd_level(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930443
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200444 error = fujitsu_backlight_register(device);
Michał Kępień07acf622017-04-05 08:49:00 +0200445 if (error)
446 return error;
Michał Kępieńaea31372017-03-10 11:50:35 +0100447
Julia Lawallb30bb892013-12-29 23:47:36 +0100448 return 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930449}
450
Jonathan Woithe20b93732008-06-11 10:14:56 +0930451/* Brightness notify */
452
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100453static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930454{
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200455 struct fujitsu_bl *priv = acpi_driver_data(device);
Michał Kępieńf2252672017-03-20 10:32:19 +0100456 int oldb, newb;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930457
Michał Kępień5efc8002017-03-01 07:42:53 +0100458 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930459 vdbg_printk(FUJLAPTOP_DBG_WARN,
460 "unsupported event [0x%x]\n", event);
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200461 sparse_keymap_report_event(priv->input, -1, 1, true);
Michał Kępień5efc8002017-03-01 07:42:53 +0100462 return;
463 }
464
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200465 oldb = priv->brightness_level;
466 get_lcd_level(device);
467 newb = priv->brightness_level;
Michał Kępień5efc8002017-03-01 07:42:53 +0100468
Michał Kępień5c495d62017-04-05 08:49:10 +0200469 vdbg_printk(FUJLAPTOP_DBG_TRACE, "brightness button event [%i -> %i]\n",
470 oldb, newb);
Michał Kępień5efc8002017-03-01 07:42:53 +0100471
Michał Kępieńd2aa3ae2017-03-01 07:42:54 +0100472 if (oldb == newb)
473 return;
474
Michał Kępieńb4bb0cf2017-04-05 08:49:06 +0200475 if (!disable_brightness_adjust)
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200476 set_lcd_level(device, newb);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930477
Michał Kępieńf2db7c62017-05-19 09:44:43 +0200478 sparse_keymap_report_event(priv->input, oldb < newb, 1, true);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930479}
480
481/* ACPI device for hotkey handling */
482
Michał Kępień527483a2017-03-20 10:32:22 +0100483static const struct key_entry keymap_default[] = {
484 { KE_KEY, KEY1_CODE, { KEY_PROG1 } },
485 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
486 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
487 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
488 { KE_KEY, KEY5_CODE, { KEY_RFKILL } },
489 { KE_KEY, BIT(26), { KEY_TOUCHPAD_TOGGLE } },
490 { KE_END, 0 }
491};
492
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100493static const struct key_entry keymap_s64x0[] = {
494 { KE_KEY, KEY1_CODE, { KEY_SCREENLOCK } }, /* "Lock" */
495 { KE_KEY, KEY2_CODE, { KEY_HELP } }, /* "Mobility Center */
496 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
497 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
498 { KE_END, 0 }
499};
500
501static const struct key_entry keymap_p8010[] = {
502 { KE_KEY, KEY1_CODE, { KEY_HELP } }, /* "Support" */
503 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
504 { KE_KEY, KEY3_CODE, { KEY_SWITCHVIDEOMODE } }, /* "Presentation" */
505 { KE_KEY, KEY4_CODE, { KEY_WWW } }, /* "WWW" */
506 { KE_END, 0 }
507};
508
Michał Kępień527483a2017-03-20 10:32:22 +0100509static const struct key_entry *keymap = keymap_default;
510
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100511static int fujitsu_laptop_dmi_keymap_override(const struct dmi_system_id *id)
512{
513 pr_info("Identified laptop model '%s'\n", id->ident);
514 keymap = id->driver_data;
515 return 1;
516}
517
518static const struct dmi_system_id fujitsu_laptop_dmi_table[] = {
519 {
520 .callback = fujitsu_laptop_dmi_keymap_override,
521 .ident = "Fujitsu Siemens S6410",
522 .matches = {
523 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
524 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
525 },
526 .driver_data = (void *)keymap_s64x0
527 },
528 {
529 .callback = fujitsu_laptop_dmi_keymap_override,
530 .ident = "Fujitsu Siemens S6420",
531 .matches = {
532 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
533 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6420"),
534 },
535 .driver_data = (void *)keymap_s64x0
536 },
537 {
538 .callback = fujitsu_laptop_dmi_keymap_override,
539 .ident = "Fujitsu LifeBook P8010",
540 .matches = {
541 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
542 DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
543 },
544 .driver_data = (void *)keymap_p8010
545 },
546 {}
547};
548
Michał Kępień11182db2017-03-20 10:32:20 +0100549static int acpi_fujitsu_laptop_input_setup(struct acpi_device *device)
550{
Michał Kępień7ec3b542017-05-19 09:44:41 +0200551 struct fujitsu_laptop *priv = acpi_driver_data(device);
Michał Kępień527483a2017-03-20 10:32:22 +0100552 int ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100553
Michał Kępień7ec3b542017-05-19 09:44:41 +0200554 priv->input = devm_input_allocate_device(&device->dev);
555 if (!priv->input)
Michał Kępień11182db2017-03-20 10:32:20 +0100556 return -ENOMEM;
557
Michał Kępień7ec3b542017-05-19 09:44:41 +0200558 snprintf(priv->phys, sizeof(priv->phys), "%s/video/input0",
559 acpi_device_hid(device));
Michał Kępień11182db2017-03-20 10:32:20 +0100560
Michał Kępień7ec3b542017-05-19 09:44:41 +0200561 priv->input->name = acpi_device_name(device);
562 priv->input->phys = priv->phys;
563 priv->input->id.bustype = BUS_HOST;
564 priv->input->id.product = 0x06;
Michał Kępień11182db2017-03-20 10:32:20 +0100565
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100566 dmi_check_system(fujitsu_laptop_dmi_table);
Michał Kępień7ec3b542017-05-19 09:44:41 +0200567 ret = sparse_keymap_setup(priv->input, keymap, NULL);
Michał Kępień527483a2017-03-20 10:32:22 +0100568 if (ret)
569 return ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100570
Michał Kępień7ec3b542017-05-19 09:44:41 +0200571 return input_register_device(priv->input);
Michał Kępień11182db2017-03-20 10:32:20 +0100572}
573
Michał Kępieńd811b512017-03-14 11:26:29 +0100574static int fujitsu_laptop_platform_add(void)
575{
576 int ret;
577
Michał Kępień979800e2017-03-14 11:26:31 +0100578 fujitsu_laptop->pf_device = platform_device_alloc("fujitsu-laptop", -1);
579 if (!fujitsu_laptop->pf_device)
Michał Kępieńd811b512017-03-14 11:26:29 +0100580 return -ENOMEM;
581
Michał Kępień979800e2017-03-14 11:26:31 +0100582 ret = platform_device_add(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100583 if (ret)
584 goto err_put_platform_device;
585
Michał Kępień979800e2017-03-14 11:26:31 +0100586 ret = sysfs_create_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100587 &fujitsu_pf_attribute_group);
588 if (ret)
589 goto err_del_platform_device;
590
591 return 0;
592
593err_del_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100594 platform_device_del(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100595err_put_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100596 platform_device_put(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100597
598 return ret;
599}
600
601static void fujitsu_laptop_platform_remove(void)
602{
Michał Kępień979800e2017-03-14 11:26:31 +0100603 sysfs_remove_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100604 &fujitsu_pf_attribute_group);
Michał Kępień979800e2017-03-14 11:26:31 +0100605 platform_device_unregister(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100606}
607
Michał Kępieńe33ca452017-04-07 15:07:10 +0200608static int logolamp_set(struct led_classdev *cdev,
609 enum led_brightness brightness)
610{
611 int poweron = FUNC_LED_ON, always = FUNC_LED_ON;
612 int ret;
613
614 if (brightness < LED_HALF)
615 poweron = FUNC_LED_OFF;
616
617 if (brightness < LED_FULL)
618 always = FUNC_LED_OFF;
619
620 ret = call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_POWERON, poweron);
621 if (ret < 0)
622 return ret;
623
624 return call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_ALWAYS, always);
625}
626
627static enum led_brightness logolamp_get(struct led_classdev *cdev)
628{
629 int ret;
630
631 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_ALWAYS, 0x0);
632 if (ret == FUNC_LED_ON)
633 return LED_FULL;
634
635 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_POWERON, 0x0);
636 if (ret == FUNC_LED_ON)
637 return LED_HALF;
638
639 return LED_OFF;
640}
641
642static struct led_classdev logolamp_led = {
643 .name = "fujitsu::logolamp",
644 .brightness_set_blocking = logolamp_set,
645 .brightness_get = logolamp_get
646};
647
648static int kblamps_set(struct led_classdev *cdev,
649 enum led_brightness brightness)
650{
651 if (brightness >= LED_FULL)
652 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
653 FUNC_LED_ON);
654 else
655 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
656 FUNC_LED_OFF);
657}
658
659static enum led_brightness kblamps_get(struct led_classdev *cdev)
660{
661 enum led_brightness brightness = LED_OFF;
662
663 if (call_fext_func(FUNC_LEDS, 0x2, KEYBOARD_LAMPS, 0x0) == FUNC_LED_ON)
664 brightness = LED_FULL;
665
666 return brightness;
667}
668
669static struct led_classdev kblamps_led = {
670 .name = "fujitsu::kblamps",
671 .brightness_set_blocking = kblamps_set,
672 .brightness_get = kblamps_get
673};
674
675static int radio_led_set(struct led_classdev *cdev,
676 enum led_brightness brightness)
677{
678 if (brightness >= LED_FULL)
679 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON,
680 RADIO_LED_ON);
681 else
682 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON, 0x0);
683}
684
685static enum led_brightness radio_led_get(struct led_classdev *cdev)
686{
687 enum led_brightness brightness = LED_OFF;
688
689 if (call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0) & RADIO_LED_ON)
690 brightness = LED_FULL;
691
692 return brightness;
693}
694
695static struct led_classdev radio_led = {
696 .name = "fujitsu::radio_led",
697 .brightness_set_blocking = radio_led_set,
698 .brightness_get = radio_led_get,
699 .default_trigger = "rfkill-any"
700};
701
702static int eco_led_set(struct led_classdev *cdev,
703 enum led_brightness brightness)
704{
705 int curr;
706
707 curr = call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0);
708 if (brightness >= LED_FULL)
709 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
710 curr | ECO_LED_ON);
711 else
712 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
713 curr & ~ECO_LED_ON);
714}
715
716static enum led_brightness eco_led_get(struct led_classdev *cdev)
717{
718 enum led_brightness brightness = LED_OFF;
719
720 if (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) & ECO_LED_ON)
721 brightness = LED_FULL;
722
723 return brightness;
724}
725
726static struct led_classdev eco_led = {
727 .name = "fujitsu::eco_led",
728 .brightness_set_blocking = eco_led_set,
729 .brightness_get = eco_led_get
730};
731
Michał Kępień81f68212017-04-07 15:07:11 +0200732static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930733{
Michał Kępień30943e12017-04-07 15:07:12 +0200734 int result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200735
736 if (call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
Michał Kępień81f68212017-04-07 15:07:11 +0200737 result = devm_led_classdev_register(&device->dev,
738 &logolamp_led);
739 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200740 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200741 }
742
743 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
744 (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200745 result = devm_led_classdev_register(&device->dev, &kblamps_led);
746 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200747 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200748 }
749
750 /*
751 * BTNI bit 24 seems to indicate the presence of a radio toggle
752 * button in place of a slide switch, and all such machines appear
753 * to also have an RF LED. Therefore use bit 24 as an indicator
754 * that an RF LED is present.
755 */
756 if (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) & BIT(24)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200757 result = devm_led_classdev_register(&device->dev, &radio_led);
758 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200759 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200760 }
761
762 /* Support for eco led is not always signaled in bit corresponding
763 * to the bit used to control the led. According to the DSDT table,
764 * bit 14 seems to indicate presence of said led as well.
765 * Confirm by testing the status.
766 */
767 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
768 (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200769 result = devm_led_classdev_register(&device->dev, &eco_led);
770 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200771 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200772 }
773
Michał Kępień30943e12017-04-07 15:07:12 +0200774 return 0;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200775}
776
777static int acpi_fujitsu_laptop_add(struct acpi_device *device)
778{
Jonathan Woithe20b93732008-06-11 10:14:56 +0930779 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930780 int error;
781 int i;
782
783 if (!device)
784 return -EINVAL;
785
Alan Jenkins6942eab2017-02-08 14:46:25 +0100786 fujitsu_laptop->acpi_handle = device->handle;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930787 sprintf(acpi_device_name(device), "%s",
Alan Jenkins6942eab2017-02-08 14:46:25 +0100788 ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930789 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Alan Jenkins6942eab2017-02-08 14:46:25 +0100790 device->driver_data = fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930791
Jonathan Woithe20b93732008-06-11 10:14:56 +0930792 /* kfifo */
Alan Jenkins6942eab2017-02-08 14:46:25 +0100793 spin_lock_init(&fujitsu_laptop->fifo_lock);
794 error = kfifo_alloc(&fujitsu_laptop->fifo, RINGBUFFERSIZE * sizeof(int),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800795 GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800796 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700797 pr_err("kfifo_alloc failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930798 goto err_stop;
799 }
800
Michał Kępień11182db2017-03-20 10:32:20 +0100801 error = acpi_fujitsu_laptop_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930802 if (error)
Michał Kępień11182db2017-03-20 10:32:20 +0100803 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930804
Alan Jenkins6942eab2017-02-08 14:46:25 +0100805 error = acpi_bus_update_power(fujitsu_laptop->acpi_handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100806 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700807 pr_err("Error reading power state\n");
Michał Kępieńf66735f2017-03-20 10:32:21 +0100808 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930809 }
810
Joe Perches77bad7c2011-03-29 15:21:39 -0700811 pr_info("ACPI: %s [%s] (%s)\n",
812 acpi_device_name(device), acpi_device_bid(device),
813 !device->power.state ? "on" : "off");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930814
Alan Jenkins6942eab2017-02-08 14:46:25 +0100815 fujitsu_laptop->dev = device;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930816
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800817 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930818 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
819 if (ACPI_FAILURE
820 (acpi_evaluate_object
821 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700822 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930823 }
824
Tony Vroon3a407082008-12-31 18:19:59 +0000825 i = 0;
826 while (call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0
827 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE)
828 ; /* No action, result is discarded */
Jonathan Woithe20b93732008-06-11 10:14:56 +0930829 vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
830
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100831 fujitsu_laptop->flags_supported =
832 call_fext_func(FUNC_FLAGS, 0x0, 0x0, 0x0);
Tony Vroon4898c2b2009-02-02 11:11:10 +0000833
834 /* Make sure our bitmask of supported functions is cleared if the
835 RFKILL function block is not implemented, like on the S7020. */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100836 if (fujitsu_laptop->flags_supported == UNSUPPORTED_CMD)
837 fujitsu_laptop->flags_supported = 0;
Tony Vroon4898c2b2009-02-02 11:11:10 +0000838
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100839 if (fujitsu_laptop->flags_supported)
840 fujitsu_laptop->flags_state =
841 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
Tony Vroon3a407082008-12-31 18:19:59 +0000842
843 /* Suspect this is a keymap of the application panel, print it */
Joe Perches77bad7c2011-03-29 15:21:39 -0700844 pr_info("BTNI: [0x%x]\n", call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0));
Tony Vroon3a407082008-12-31 18:19:59 +0000845
Michał Kępień1877e262017-03-10 11:50:34 +0100846 /* Sync backlight power status */
Michał Kępień679374e2017-05-19 09:44:42 +0200847 if (fujitsu_bl && fujitsu_bl->bl_device &&
Michał Kępieńaea31372017-03-10 11:50:35 +0100848 acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Michał Kępień1877e262017-03-10 11:50:34 +0100849 if (call_fext_func(FUNC_BACKLIGHT, 0x2, 0x4, 0x0) == 3)
850 fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
851 else
852 fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
853 }
854
Michał Kępieńd1c70732017-04-07 15:07:13 +0200855 error = acpi_fujitsu_laptop_leds_register(device);
856 if (error)
857 goto err_free_fifo;
858
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100859 error = fujitsu_laptop_platform_add();
860 if (error)
Michał Kępieńf66735f2017-03-20 10:32:21 +0100861 goto err_free_fifo;
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100862
Michał Kępień7adb7b12017-04-07 15:07:09 +0200863 return 0;
Michał Kępień4f625682016-04-12 22:06:34 +0930864
Bjorn Helgaasb4ec0272009-04-07 15:37:22 +0000865err_free_fifo:
Alan Jenkins6942eab2017-02-08 14:46:25 +0100866 kfifo_free(&fujitsu_laptop->fifo);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930867err_stop:
Julia Lawallb30bb892013-12-29 23:47:36 +0100868 return error;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930869}
870
Alan Jenkins6942eab2017-02-08 14:46:25 +0100871static int acpi_fujitsu_laptop_remove(struct acpi_device *device)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930872{
Michał Kępień7ec3b542017-05-19 09:44:41 +0200873 struct fujitsu_laptop *priv = acpi_driver_data(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930874
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100875 fujitsu_laptop_platform_remove();
876
Michał Kępień7ec3b542017-05-19 09:44:41 +0200877 kfifo_free(&priv->fifo);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930878
879 return 0;
880}
881
Michał Kępień527483a2017-03-20 10:32:22 +0100882static void acpi_fujitsu_laptop_press(int scancode)
Michał Kępień2451d192017-01-11 09:59:31 +0100883{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100884 struct input_dev *input = fujitsu_laptop->input;
Michał Kępień2451d192017-01-11 09:59:31 +0100885 int status;
886
Alan Jenkins6942eab2017-02-08 14:46:25 +0100887 status = kfifo_in_locked(&fujitsu_laptop->fifo,
Michał Kępień527483a2017-03-20 10:32:22 +0100888 (unsigned char *)&scancode, sizeof(scancode),
Alan Jenkins6942eab2017-02-08 14:46:25 +0100889 &fujitsu_laptop->fifo_lock);
Michał Kępień527483a2017-03-20 10:32:22 +0100890 if (status != sizeof(scancode)) {
Michał Kępień2451d192017-01-11 09:59:31 +0100891 vdbg_printk(FUJLAPTOP_DBG_WARN,
Michał Kępień527483a2017-03-20 10:32:22 +0100892 "Could not push scancode [0x%x]\n", scancode);
Michał Kępieńa28c7e92017-01-11 09:59:33 +0100893 return;
Michał Kępień2451d192017-01-11 09:59:31 +0100894 }
Michał Kępień527483a2017-03-20 10:32:22 +0100895 sparse_keymap_report_event(input, scancode, 1, false);
Michał Kępieńa28c7e92017-01-11 09:59:33 +0100896 vdbg_printk(FUJLAPTOP_DBG_TRACE,
Michał Kępień527483a2017-03-20 10:32:22 +0100897 "Push scancode into ringbuffer [0x%x]\n", scancode);
Michał Kępień2451d192017-01-11 09:59:31 +0100898}
899
Alan Jenkins6942eab2017-02-08 14:46:25 +0100900static void acpi_fujitsu_laptop_release(void)
Michał Kępień2451d192017-01-11 09:59:31 +0100901{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100902 struct input_dev *input = fujitsu_laptop->input;
Michał Kępień527483a2017-03-20 10:32:22 +0100903 int scancode, status;
Michał Kępień2451d192017-01-11 09:59:31 +0100904
Michał Kępień29544f02017-01-11 09:59:32 +0100905 while (true) {
Alan Jenkins6942eab2017-02-08 14:46:25 +0100906 status = kfifo_out_locked(&fujitsu_laptop->fifo,
Michał Kępień527483a2017-03-20 10:32:22 +0100907 (unsigned char *)&scancode,
908 sizeof(scancode),
Alan Jenkins6942eab2017-02-08 14:46:25 +0100909 &fujitsu_laptop->fifo_lock);
Michał Kępień527483a2017-03-20 10:32:22 +0100910 if (status != sizeof(scancode))
Michał Kępień29544f02017-01-11 09:59:32 +0100911 return;
Michał Kępień527483a2017-03-20 10:32:22 +0100912 sparse_keymap_report_event(input, scancode, 0, false);
Michał Kępień2451d192017-01-11 09:59:31 +0100913 vdbg_printk(FUJLAPTOP_DBG_TRACE,
Michał Kępień527483a2017-03-20 10:32:22 +0100914 "Pop scancode from ringbuffer [0x%x]\n", scancode);
Michał Kępień2451d192017-01-11 09:59:31 +0100915 }
916}
917
Alan Jenkins6942eab2017-02-08 14:46:25 +0100918static void acpi_fujitsu_laptop_notify(struct acpi_device *device, u32 event)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930919{
920 struct input_dev *input;
Michał Kępień527483a2017-03-20 10:32:22 +0100921 int scancode, i = 0;
922 unsigned int irb;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930923
Alan Jenkins6942eab2017-02-08 14:46:25 +0100924 input = fujitsu_laptop->input;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930925
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100926 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930927 vdbg_printk(FUJLAPTOP_DBG_WARN,
928 "Unsupported event [0x%x]\n", event);
Michał Kępień527483a2017-03-20 10:32:22 +0100929 sparse_keymap_report_event(input, -1, 1, true);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100930 return;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930931 }
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100932
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100933 if (fujitsu_laptop->flags_supported)
934 fujitsu_laptop->flags_state =
935 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100936
Michał Kępień527483a2017-03-20 10:32:22 +0100937 while ((irb = call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0)) != 0 &&
938 i++ < MAX_HOTKEY_RINGBUFFER_SIZE) {
939 scancode = irb & 0x4ff;
940 if (sparse_keymap_entry_from_scancode(input, scancode))
941 acpi_fujitsu_laptop_press(scancode);
942 else if (scancode == 0)
943 acpi_fujitsu_laptop_release();
944 else
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100945 vdbg_printk(FUJLAPTOP_DBG_WARN,
946 "Unknown GIRB result [%x]\n", irb);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100947 }
948
949 /* On some models (first seen on the Skylake-based Lifebook
950 * E736/E746/E756), the touchpad toggle hotkey (Fn+F4) is
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100951 * handled in software; its state is queried using FUNC_FLAGS
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100952 */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100953 if ((fujitsu_laptop->flags_supported & BIT(26)) &&
Michał Kępień527483a2017-03-20 10:32:22 +0100954 (call_fext_func(FUNC_FLAGS, 0x1, 0x0, 0x0) & BIT(26)))
955 sparse_keymap_report_event(input, BIT(26), 1, true);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930956}
957
958/* Initialization */
959
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100960static const struct acpi_device_id fujitsu_bl_device_ids[] = {
961 {ACPI_FUJITSU_BL_HID, 0},
Jonathan Woithed0482532007-08-29 15:58:19 +0930962 {"", 0},
963};
964
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100965static struct acpi_driver acpi_fujitsu_bl_driver = {
966 .name = ACPI_FUJITSU_BL_DRIVER_NAME,
Jonathan Woithed0482532007-08-29 15:58:19 +0930967 .class = ACPI_FUJITSU_CLASS,
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100968 .ids = fujitsu_bl_device_ids,
Jonathan Woithed0482532007-08-29 15:58:19 +0930969 .ops = {
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100970 .add = acpi_fujitsu_bl_add,
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100971 .notify = acpi_fujitsu_bl_notify,
Jonathan Woithed0482532007-08-29 15:58:19 +0930972 },
973};
974
Alan Jenkins6942eab2017-02-08 14:46:25 +0100975static const struct acpi_device_id fujitsu_laptop_device_ids[] = {
976 {ACPI_FUJITSU_LAPTOP_HID, 0},
Jonathan Woithe20b93732008-06-11 10:14:56 +0930977 {"", 0},
978};
979
Alan Jenkins6942eab2017-02-08 14:46:25 +0100980static struct acpi_driver acpi_fujitsu_laptop_driver = {
981 .name = ACPI_FUJITSU_LAPTOP_DRIVER_NAME,
Jonathan Woithe20b93732008-06-11 10:14:56 +0930982 .class = ACPI_FUJITSU_CLASS,
Alan Jenkins6942eab2017-02-08 14:46:25 +0100983 .ids = fujitsu_laptop_device_ids,
Jonathan Woithe20b93732008-06-11 10:14:56 +0930984 .ops = {
Alan Jenkins6942eab2017-02-08 14:46:25 +0100985 .add = acpi_fujitsu_laptop_add,
986 .remove = acpi_fujitsu_laptop_remove,
987 .notify = acpi_fujitsu_laptop_notify,
Jonathan Woithe20b93732008-06-11 10:14:56 +0930988 },
989};
Jonathan Woithed0482532007-08-29 15:58:19 +0930990
Zhang Rui49901412014-09-09 00:21:59 +0200991static const struct acpi_device_id fujitsu_ids[] __used = {
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100992 {ACPI_FUJITSU_BL_HID, 0},
Alan Jenkins6942eab2017-02-08 14:46:25 +0100993 {ACPI_FUJITSU_LAPTOP_HID, 0},
Zhang Rui49901412014-09-09 00:21:59 +0200994 {"", 0}
995};
996MODULE_DEVICE_TABLE(acpi, fujitsu_ids);
997
Jonathan Woithed0482532007-08-29 15:58:19 +0930998static int __init fujitsu_init(void)
999{
Michał Kępieńb8d69c12017-03-10 11:50:33 +01001000 int ret;
Jonathan Woithed0482532007-08-29 15:58:19 +09301001
1002 if (acpi_disabled)
1003 return -ENODEV;
1004
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001005 ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
1006 if (ret)
Michał Kępień679374e2017-05-19 09:44:42 +02001007 return ret;
Jonathan Woithed0482532007-08-29 15:58:19 +09301008
Jonathan Woithed0482532007-08-29 15:58:19 +09301009 /* Register platform stuff */
1010
Alan Jenkins16506022017-02-08 14:46:26 +01001011 ret = platform_driver_register(&fujitsu_pf_driver);
Jonathan Woithe20b93732008-06-11 10:14:56 +09301012 if (ret)
Michał Kępieńc33f4c02017-03-14 11:26:30 +01001013 goto err_unregister_acpi;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301014
Alan Jenkins6942eab2017-02-08 14:46:25 +01001015 /* Register laptop driver */
Jonathan Woithe20b93732008-06-11 10:14:56 +09301016
Alan Jenkins6942eab2017-02-08 14:46:25 +01001017 fujitsu_laptop = kzalloc(sizeof(struct fujitsu_laptop), GFP_KERNEL);
1018 if (!fujitsu_laptop) {
Jonathan Woithe20b93732008-06-11 10:14:56 +09301019 ret = -ENOMEM;
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001020 goto err_unregister_platform_driver;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301021 }
Jonathan Woithe20b93732008-06-11 10:14:56 +09301022
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001023 ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
1024 if (ret)
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001025 goto err_free_fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301026
Joe Perches77bad7c2011-03-29 15:21:39 -07001027 pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301028
1029 return 0;
1030
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001031err_free_fujitsu_laptop:
Alan Jenkins6942eab2017-02-08 14:46:25 +01001032 kfree(fujitsu_laptop);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001033err_unregister_platform_driver:
Alan Jenkins16506022017-02-08 14:46:26 +01001034 platform_driver_unregister(&fujitsu_pf_driver);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001035err_unregister_acpi:
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001036 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Jonathan Woithed0482532007-08-29 15:58:19 +09301037
1038 return ret;
1039}
1040
1041static void __exit fujitsu_cleanup(void)
1042{
Alan Jenkins6942eab2017-02-08 14:46:25 +01001043 acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
Tony Vroon3a407082008-12-31 18:19:59 +00001044
Alan Jenkins6942eab2017-02-08 14:46:25 +01001045 kfree(fujitsu_laptop);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301046
Alan Jenkins16506022017-02-08 14:46:26 +01001047 platform_driver_unregister(&fujitsu_pf_driver);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301048
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001049 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Jonathan Woithed0482532007-08-29 15:58:19 +09301050
Joe Perches77bad7c2011-03-29 15:21:39 -07001051 pr_info("driver unloaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301052}
1053
1054module_init(fujitsu_init);
1055module_exit(fujitsu_cleanup);
1056
Michał Kępieńe06e4832017-04-05 08:49:05 +02001057module_param(use_alt_lcd_levels, int, 0644);
1058MODULE_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 +02001059module_param(disable_brightness_adjust, bool, 0644);
1060MODULE_PARM_DESC(disable_brightness_adjust, "Disable LCD brightness adjustment");
Jonathan Woithe20b93732008-06-11 10:14:56 +09301061#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
1062module_param_named(debug, dbg_level, uint, 0644);
1063MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
1064#endif
1065
Tony Vroon3a407082008-12-31 18:19:59 +00001066MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
Jonathan Woithed0482532007-08-29 15:58:19 +09301067MODULE_DESCRIPTION("Fujitsu laptop extras support");
1068MODULE_VERSION(FUJITSU_DRIVER_VERSION);
1069MODULE_LICENSE("GPL");