blob: 63a9b98967faa52cb22e7fab0802a7fc95912c81 [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;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930156};
157
Alan Jenkins6942eab2017-02-08 14:46:25 +0100158static struct fujitsu_laptop *fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930159
Jonathan Woithe20b93732008-06-11 10:14:56 +0930160#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
161static u32 dbg_level = 0x03;
162#endif
163
Tony Vroon3a407082008-12-31 18:19:59 +0000164/* Fujitsu ACPI interface function */
165
Michał Kępieńf68e4922017-04-03 11:38:59 +0200166static int call_fext_func(int func, int op, int feature, int state)
Tony Vroon3a407082008-12-31 18:19:59 +0000167{
Tony Vroon3a407082008-12-31 18:19:59 +0000168 union acpi_object params[4] = {
Michał Kępieńf68e4922017-04-03 11:38:59 +0200169 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = func },
170 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = op },
171 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = feature },
172 { .integer.type = ACPI_TYPE_INTEGER, .integer.value = state }
Tony Vroon3a407082008-12-31 18:19:59 +0000173 };
Michał Kępieńb1066412017-04-03 11:38:57 +0200174 struct acpi_object_list arg_list = { 4, params };
Zhang Rui29c29a92013-09-03 08:32:12 +0800175 unsigned long long value;
Michał Kępieńb1066412017-04-03 11:38:57 +0200176 acpi_status status;
Tony Vroon3a407082008-12-31 18:19:59 +0000177
Michał Kępień17e23552017-04-03 11:38:58 +0200178 status = acpi_evaluate_integer(fujitsu_laptop->acpi_handle, "FUNC",
179 &arg_list, &value);
Tony Vroon3a407082008-12-31 18:19:59 +0000180 if (ACPI_FAILURE(status)) {
Michał Kępień09b29e12017-04-06 08:46:10 +0200181 vdbg_printk(FUJLAPTOP_DBG_ERROR, "Failed to evaluate FUNC\n");
Tony Vroon3a407082008-12-31 18:19:59 +0000182 return -ENODEV;
183 }
184
Michał Kępieńf68e4922017-04-03 11:38:59 +0200185 vdbg_printk(FUJLAPTOP_DBG_TRACE, "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) returned 0x%x\n",
186 func, op, feature, state, (int)value);
Zhang Rui29c29a92013-09-03 08:32:12 +0800187 return value;
Tony Vroon3a407082008-12-31 18:19:59 +0000188}
189
Jonathan Woithe20b93732008-06-11 10:14:56 +0930190/* Hardware access for LCD brightness control */
Jonathan Woithed0482532007-08-29 15:58:19 +0930191
192static int set_lcd_level(int level)
193{
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:
199 if (acpi_has_method(fujitsu_bl->acpi_handle, "SBL2"))
200 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
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100215 if (level < 0 || level >= fujitsu_bl->max_brightness)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930216 return -EINVAL;
217
Michał Kępieńa8779c32017-04-05 08:49:03 +0200218 status = acpi_execute_simple_method(fujitsu_bl->acpi_handle, method,
219 level);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930220 if (ACPI_FAILURE(status)) {
Michał Kępieńa8779c32017-04-05 08:49:03 +0200221 vdbg_printk(FUJLAPTOP_DBG_ERROR, "Failed to evaluate %s\n",
222 method);
Jonathan Woithed0482532007-08-29 15:58:19 +0930223 return -ENODEV;
224 }
225
Michał Kępieńbd079a22017-04-05 08:49:04 +0200226 fujitsu_bl->brightness_level = level;
227
Jonathan Woithed0482532007-08-29 15:58:19 +0930228 return 0;
229}
230
231static int get_lcd_level(void)
232{
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
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100238 status = acpi_evaluate_integer(fujitsu_bl->acpi_handle, "GBLL", NULL,
239 &state);
Jonathan Woithe3b1c37c2009-12-23 09:19:42 +1030240 if (ACPI_FAILURE(status))
241 return 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930242
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100243 fujitsu_bl->brightness_level = state & 0x0fffffff;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930244
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100245 return fujitsu_bl->brightness_level;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930246}
247
248static int get_max_brightness(void)
249{
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
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100255 status = acpi_evaluate_integer(fujitsu_bl->acpi_handle, "RBLL", NULL,
256 &state);
Jonathan Woithe3b1c37c2009-12-23 09:19:42 +1030257 if (ACPI_FAILURE(status))
258 return -1;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930259
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100260 fujitsu_bl->max_brightness = state;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930261
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100262 return fujitsu_bl->max_brightness;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930263}
264
Jonathan Woithed0482532007-08-29 15:58:19 +0930265/* Backlight device stuff */
266
267static int bl_get_brightness(struct backlight_device *b)
268{
Michał Kępień5959ddd2017-04-05 08:49:09 +0200269 return b->props.power == FB_BLANK_POWERDOWN ? 0 : get_lcd_level();
Jonathan Woithed0482532007-08-29 15:58:19 +0930270}
271
272static int bl_update_status(struct backlight_device *b)
273{
Michael Karchere8549e22015-01-18 20:28:46 +0100274 if (b->props.power == FB_BLANK_POWERDOWN)
Michał Kępieńf7c4c3f2017-04-05 08:49:07 +0200275 call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x3);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930276 else
Michał Kępieńf7c4c3f2017-04-05 08:49:07 +0200277 call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x0);
Tony Vroon3a407082008-12-31 18:19:59 +0000278
Michał Kępieńd75a4a92017-04-05 08:49:08 +0200279 return set_lcd_level(b->props.brightness);
Jonathan Woithed0482532007-08-29 15:58:19 +0930280}
281
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100282static const struct backlight_ops fujitsu_bl_ops = {
Jonathan Woithed0482532007-08-29 15:58:19 +0930283 .get_brightness = bl_get_brightness,
284 .update_status = bl_update_status,
285};
286
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100287static ssize_t lid_show(struct device *dev, struct device_attribute *attr,
288 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000289{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100290 if (!(fujitsu_laptop->flags_supported & FLAG_LID))
Tony Vroon3a407082008-12-31 18:19:59 +0000291 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100292 if (fujitsu_laptop->flags_state & FLAG_LID)
Tony Vroon3a407082008-12-31 18:19:59 +0000293 return sprintf(buf, "open\n");
294 else
295 return sprintf(buf, "closed\n");
296}
297
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100298static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
299 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000300{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100301 if (!(fujitsu_laptop->flags_supported & FLAG_DOCK))
Tony Vroon3a407082008-12-31 18:19:59 +0000302 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100303 if (fujitsu_laptop->flags_state & FLAG_DOCK)
Tony Vroon3a407082008-12-31 18:19:59 +0000304 return sprintf(buf, "docked\n");
305 else
306 return sprintf(buf, "undocked\n");
307}
308
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100309static ssize_t radios_show(struct device *dev, struct device_attribute *attr,
310 char *buf)
Tony Vroon3a407082008-12-31 18:19:59 +0000311{
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100312 if (!(fujitsu_laptop->flags_supported & FLAG_RFKILL))
Tony Vroon3a407082008-12-31 18:19:59 +0000313 return sprintf(buf, "unknown\n");
Alan Jenkinsd3dd4482017-02-08 14:46:28 +0100314 if (fujitsu_laptop->flags_state & FLAG_RFKILL)
Tony Vroon3a407082008-12-31 18:19:59 +0000315 return sprintf(buf, "on\n");
316 else
317 return sprintf(buf, "killed\n");
318}
319
Michał Kępieńb0c4b9c2017-03-14 11:26:28 +0100320static DEVICE_ATTR_RO(lid);
321static DEVICE_ATTR_RO(dock);
322static DEVICE_ATTR_RO(radios);
Jonathan Woithed0482532007-08-29 15:58:19 +0930323
Alan Jenkins16506022017-02-08 14:46:26 +0100324static struct attribute *fujitsu_pf_attributes[] = {
Tony Vroon3a407082008-12-31 18:19:59 +0000325 &dev_attr_lid.attr,
326 &dev_attr_dock.attr,
327 &dev_attr_radios.attr,
Jonathan Woithed0482532007-08-29 15:58:19 +0930328 NULL
329};
330
Alan Jenkins16506022017-02-08 14:46:26 +0100331static struct attribute_group fujitsu_pf_attribute_group = {
332 .attrs = fujitsu_pf_attributes
Jonathan Woithed0482532007-08-29 15:58:19 +0930333};
334
Alan Jenkins16506022017-02-08 14:46:26 +0100335static struct platform_driver fujitsu_pf_driver = {
Jonathan Woithed0482532007-08-29 15:58:19 +0930336 .driver = {
337 .name = "fujitsu-laptop",
Jonathan Woithed0482532007-08-29 15:58:19 +0930338 }
339};
340
Jonathan Woithe20b93732008-06-11 10:14:56 +0930341/* ACPI device for LCD brightness control */
Jonathan Woithed0482532007-08-29 15:58:19 +0930342
Michał Kępieńf2252672017-03-20 10:32:19 +0100343static const struct key_entry keymap_backlight[] = {
344 { KE_KEY, true, { KEY_BRIGHTNESSUP } },
345 { KE_KEY, false, { KEY_BRIGHTNESSDOWN } },
346 { KE_END, 0 }
347};
348
Michał Kępień7d134e42017-03-20 10:32:17 +0100349static int acpi_fujitsu_bl_input_setup(struct acpi_device *device)
350{
Michał Kępień7ec3b542017-05-19 09:44:41 +0200351 struct fujitsu_bl *priv = acpi_driver_data(device);
Michał Kępieńf2252672017-03-20 10:32:19 +0100352 int ret;
Michał Kępień7d134e42017-03-20 10:32:17 +0100353
Michał Kępień7ec3b542017-05-19 09:44:41 +0200354 priv->input = devm_input_allocate_device(&device->dev);
355 if (!priv->input)
Michał Kępień7d134e42017-03-20 10:32:17 +0100356 return -ENOMEM;
357
Michał Kępień7ec3b542017-05-19 09:44:41 +0200358 snprintf(priv->phys, sizeof(priv->phys), "%s/video/input0",
359 acpi_device_hid(device));
Michał Kępień7d134e42017-03-20 10:32:17 +0100360
Michał Kępień7ec3b542017-05-19 09:44:41 +0200361 priv->input->name = acpi_device_name(device);
362 priv->input->phys = priv->phys;
363 priv->input->id.bustype = BUS_HOST;
364 priv->input->id.product = 0x06;
Michał Kępieńf2252672017-03-20 10:32:19 +0100365
Michał Kępień7ec3b542017-05-19 09:44:41 +0200366 ret = sparse_keymap_setup(priv->input, keymap_backlight, NULL);
Michał Kępieńf2252672017-03-20 10:32:19 +0100367 if (ret)
368 return ret;
Michał Kępień7d134e42017-03-20 10:32:17 +0100369
Michał Kępień7ec3b542017-05-19 09:44:41 +0200370 return input_register_device(priv->input);
Michał Kępień7d134e42017-03-20 10:32:17 +0100371}
372
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200373static int fujitsu_backlight_register(struct acpi_device *device)
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100374{
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200375 const struct backlight_properties props = {
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100376 .brightness = fujitsu_bl->brightness_level,
377 .max_brightness = fujitsu_bl->max_brightness - 1,
378 .type = BACKLIGHT_PLATFORM
379 };
380 struct backlight_device *bd;
381
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200382 bd = devm_backlight_device_register(&device->dev, "fujitsu-laptop",
383 &device->dev, NULL,
384 &fujitsu_bl_ops, &props);
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100385 if (IS_ERR(bd))
386 return PTR_ERR(bd);
387
388 fujitsu_bl->bl_device = bd;
389
390 return 0;
391}
392
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100393static int acpi_fujitsu_bl_add(struct acpi_device *device)
Jonathan Woithed0482532007-08-29 15:58:19 +0930394{
Michał Kępień679374e2017-05-19 09:44:42 +0200395 struct fujitsu_bl *priv;
Jonathan Woithed0482532007-08-29 15:58:19 +0930396 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930397 int error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930398
Michał Kępień07acf622017-04-05 08:49:00 +0200399 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
400 return -ENODEV;
401
Jonathan Woithed0482532007-08-29 15:58:19 +0930402 if (!device)
403 return -EINVAL;
404
Michał Kępień679374e2017-05-19 09:44:42 +0200405 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
406 if (!priv)
407 return -ENOMEM;
408
409 fujitsu_bl = priv;
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100410 fujitsu_bl->acpi_handle = device->handle;
411 sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_BL_DEVICE_NAME);
Jonathan Woithed0482532007-08-29 15:58:19 +0930412 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Michał Kępień679374e2017-05-19 09:44:42 +0200413 device->driver_data = priv;
Jonathan Woithed0482532007-08-29 15:58:19 +0930414
Michał Kępień7d134e42017-03-20 10:32:17 +0100415 error = acpi_fujitsu_bl_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930416 if (error)
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100417 return error;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930418
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100419 error = acpi_bus_update_power(fujitsu_bl->acpi_handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100420 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700421 pr_err("Error reading power state\n");
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100422 return error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930423 }
424
Joe Perches77bad7c2011-03-29 15:21:39 -0700425 pr_info("ACPI: %s [%s] (%s)\n",
Jonathan Woithed0482532007-08-29 15:58:19 +0930426 acpi_device_name(device), acpi_device_bid(device),
427 !device->power.state ? "on" : "off");
428
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800429 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930430 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
431 if (ACPI_FAILURE
432 (acpi_evaluate_object
433 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700434 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930435 }
436
Jonathan Woithe20b93732008-06-11 10:14:56 +0930437 if (get_max_brightness() <= 0)
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100438 fujitsu_bl->max_brightness = FUJITSU_LCD_N_LEVELS;
Tony Vroonf87a1a52009-01-07 10:11:24 +0000439 get_lcd_level();
Jonathan Woithe20b93732008-06-11 10:14:56 +0930440
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200441 error = fujitsu_backlight_register(device);
Michał Kępień07acf622017-04-05 08:49:00 +0200442 if (error)
443 return error;
Michał Kępieńaea31372017-03-10 11:50:35 +0100444
Julia Lawallb30bb892013-12-29 23:47:36 +0100445 return 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930446}
447
Jonathan Woithe20b93732008-06-11 10:14:56 +0930448/* Brightness notify */
449
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100450static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930451{
452 struct input_dev *input;
Michał Kępieńf2252672017-03-20 10:32:19 +0100453 int oldb, newb;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930454
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100455 input = fujitsu_bl->input;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930456
Michał Kępień5efc8002017-03-01 07:42:53 +0100457 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930458 vdbg_printk(FUJLAPTOP_DBG_WARN,
459 "unsupported event [0x%x]\n", event);
Michał Kępieńf2252672017-03-20 10:32:19 +0100460 sparse_keymap_report_event(input, -1, 1, true);
Michał Kępień5efc8002017-03-01 07:42:53 +0100461 return;
462 }
463
Michał Kępień5efc8002017-03-01 07:42:53 +0100464 oldb = fujitsu_bl->brightness_level;
465 get_lcd_level();
466 newb = fujitsu_bl->brightness_level;
467
Michał Kępień5c495d62017-04-05 08:49:10 +0200468 vdbg_printk(FUJLAPTOP_DBG_TRACE, "brightness button event [%i -> %i]\n",
469 oldb, newb);
Michał Kępień5efc8002017-03-01 07:42:53 +0100470
Michał Kępieńd2aa3ae2017-03-01 07:42:54 +0100471 if (oldb == newb)
472 return;
473
Michał Kępieńb4bb0cf2017-04-05 08:49:06 +0200474 if (!disable_brightness_adjust)
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200475 set_lcd_level(newb);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930476
Michał Kępieńf2252672017-03-20 10:32:19 +0100477 sparse_keymap_report_event(input, oldb < newb, 1, true);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930478}
479
480/* ACPI device for hotkey handling */
481
Michał Kępień527483a2017-03-20 10:32:22 +0100482static const struct key_entry keymap_default[] = {
483 { KE_KEY, KEY1_CODE, { KEY_PROG1 } },
484 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
485 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
486 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
487 { KE_KEY, KEY5_CODE, { KEY_RFKILL } },
488 { KE_KEY, BIT(26), { KEY_TOUCHPAD_TOGGLE } },
489 { KE_END, 0 }
490};
491
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100492static const struct key_entry keymap_s64x0[] = {
493 { KE_KEY, KEY1_CODE, { KEY_SCREENLOCK } }, /* "Lock" */
494 { KE_KEY, KEY2_CODE, { KEY_HELP } }, /* "Mobility Center */
495 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
496 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
497 { KE_END, 0 }
498};
499
500static const struct key_entry keymap_p8010[] = {
501 { KE_KEY, KEY1_CODE, { KEY_HELP } }, /* "Support" */
502 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
503 { KE_KEY, KEY3_CODE, { KEY_SWITCHVIDEOMODE } }, /* "Presentation" */
504 { KE_KEY, KEY4_CODE, { KEY_WWW } }, /* "WWW" */
505 { KE_END, 0 }
506};
507
Michał Kępień527483a2017-03-20 10:32:22 +0100508static const struct key_entry *keymap = keymap_default;
509
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100510static int fujitsu_laptop_dmi_keymap_override(const struct dmi_system_id *id)
511{
512 pr_info("Identified laptop model '%s'\n", id->ident);
513 keymap = id->driver_data;
514 return 1;
515}
516
517static const struct dmi_system_id fujitsu_laptop_dmi_table[] = {
518 {
519 .callback = fujitsu_laptop_dmi_keymap_override,
520 .ident = "Fujitsu Siemens S6410",
521 .matches = {
522 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
523 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
524 },
525 .driver_data = (void *)keymap_s64x0
526 },
527 {
528 .callback = fujitsu_laptop_dmi_keymap_override,
529 .ident = "Fujitsu Siemens S6420",
530 .matches = {
531 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
532 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6420"),
533 },
534 .driver_data = (void *)keymap_s64x0
535 },
536 {
537 .callback = fujitsu_laptop_dmi_keymap_override,
538 .ident = "Fujitsu LifeBook P8010",
539 .matches = {
540 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
541 DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
542 },
543 .driver_data = (void *)keymap_p8010
544 },
545 {}
546};
547
Michał Kępień11182db2017-03-20 10:32:20 +0100548static int acpi_fujitsu_laptop_input_setup(struct acpi_device *device)
549{
Michał Kępień7ec3b542017-05-19 09:44:41 +0200550 struct fujitsu_laptop *priv = acpi_driver_data(device);
Michał Kępień527483a2017-03-20 10:32:22 +0100551 int ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100552
Michał Kępień7ec3b542017-05-19 09:44:41 +0200553 priv->input = devm_input_allocate_device(&device->dev);
554 if (!priv->input)
Michał Kępień11182db2017-03-20 10:32:20 +0100555 return -ENOMEM;
556
Michał Kępień7ec3b542017-05-19 09:44:41 +0200557 snprintf(priv->phys, sizeof(priv->phys), "%s/video/input0",
558 acpi_device_hid(device));
Michał Kępień11182db2017-03-20 10:32:20 +0100559
Michał Kępień7ec3b542017-05-19 09:44:41 +0200560 priv->input->name = acpi_device_name(device);
561 priv->input->phys = priv->phys;
562 priv->input->id.bustype = BUS_HOST;
563 priv->input->id.product = 0x06;
Michał Kępień11182db2017-03-20 10:32:20 +0100564
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100565 dmi_check_system(fujitsu_laptop_dmi_table);
Michał Kępień7ec3b542017-05-19 09:44:41 +0200566 ret = sparse_keymap_setup(priv->input, keymap, NULL);
Michał Kępień527483a2017-03-20 10:32:22 +0100567 if (ret)
568 return ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100569
Michał Kępień7ec3b542017-05-19 09:44:41 +0200570 return input_register_device(priv->input);
Michał Kępień11182db2017-03-20 10:32:20 +0100571}
572
Michał Kępieńd811b512017-03-14 11:26:29 +0100573static int fujitsu_laptop_platform_add(void)
574{
575 int ret;
576
Michał Kępień979800e2017-03-14 11:26:31 +0100577 fujitsu_laptop->pf_device = platform_device_alloc("fujitsu-laptop", -1);
578 if (!fujitsu_laptop->pf_device)
Michał Kępieńd811b512017-03-14 11:26:29 +0100579 return -ENOMEM;
580
Michał Kępień979800e2017-03-14 11:26:31 +0100581 ret = platform_device_add(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100582 if (ret)
583 goto err_put_platform_device;
584
Michał Kępień979800e2017-03-14 11:26:31 +0100585 ret = sysfs_create_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100586 &fujitsu_pf_attribute_group);
587 if (ret)
588 goto err_del_platform_device;
589
590 return 0;
591
592err_del_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100593 platform_device_del(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100594err_put_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100595 platform_device_put(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100596
597 return ret;
598}
599
600static void fujitsu_laptop_platform_remove(void)
601{
Michał Kępień979800e2017-03-14 11:26:31 +0100602 sysfs_remove_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100603 &fujitsu_pf_attribute_group);
Michał Kępień979800e2017-03-14 11:26:31 +0100604 platform_device_unregister(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100605}
606
Michał Kępieńe33ca452017-04-07 15:07:10 +0200607static int logolamp_set(struct led_classdev *cdev,
608 enum led_brightness brightness)
609{
610 int poweron = FUNC_LED_ON, always = FUNC_LED_ON;
611 int ret;
612
613 if (brightness < LED_HALF)
614 poweron = FUNC_LED_OFF;
615
616 if (brightness < LED_FULL)
617 always = FUNC_LED_OFF;
618
619 ret = call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_POWERON, poweron);
620 if (ret < 0)
621 return ret;
622
623 return call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_ALWAYS, always);
624}
625
626static enum led_brightness logolamp_get(struct led_classdev *cdev)
627{
628 int ret;
629
630 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_ALWAYS, 0x0);
631 if (ret == FUNC_LED_ON)
632 return LED_FULL;
633
634 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_POWERON, 0x0);
635 if (ret == FUNC_LED_ON)
636 return LED_HALF;
637
638 return LED_OFF;
639}
640
641static struct led_classdev logolamp_led = {
642 .name = "fujitsu::logolamp",
643 .brightness_set_blocking = logolamp_set,
644 .brightness_get = logolamp_get
645};
646
647static int kblamps_set(struct led_classdev *cdev,
648 enum led_brightness brightness)
649{
650 if (brightness >= LED_FULL)
651 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
652 FUNC_LED_ON);
653 else
654 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
655 FUNC_LED_OFF);
656}
657
658static enum led_brightness kblamps_get(struct led_classdev *cdev)
659{
660 enum led_brightness brightness = LED_OFF;
661
662 if (call_fext_func(FUNC_LEDS, 0x2, KEYBOARD_LAMPS, 0x0) == FUNC_LED_ON)
663 brightness = LED_FULL;
664
665 return brightness;
666}
667
668static struct led_classdev kblamps_led = {
669 .name = "fujitsu::kblamps",
670 .brightness_set_blocking = kblamps_set,
671 .brightness_get = kblamps_get
672};
673
674static int radio_led_set(struct led_classdev *cdev,
675 enum led_brightness brightness)
676{
677 if (brightness >= LED_FULL)
678 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON,
679 RADIO_LED_ON);
680 else
681 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON, 0x0);
682}
683
684static enum led_brightness radio_led_get(struct led_classdev *cdev)
685{
686 enum led_brightness brightness = LED_OFF;
687
688 if (call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0) & RADIO_LED_ON)
689 brightness = LED_FULL;
690
691 return brightness;
692}
693
694static struct led_classdev radio_led = {
695 .name = "fujitsu::radio_led",
696 .brightness_set_blocking = radio_led_set,
697 .brightness_get = radio_led_get,
698 .default_trigger = "rfkill-any"
699};
700
701static int eco_led_set(struct led_classdev *cdev,
702 enum led_brightness brightness)
703{
704 int curr;
705
706 curr = call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0);
707 if (brightness >= LED_FULL)
708 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
709 curr | ECO_LED_ON);
710 else
711 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
712 curr & ~ECO_LED_ON);
713}
714
715static enum led_brightness eco_led_get(struct led_classdev *cdev)
716{
717 enum led_brightness brightness = LED_OFF;
718
719 if (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) & ECO_LED_ON)
720 brightness = LED_FULL;
721
722 return brightness;
723}
724
725static struct led_classdev eco_led = {
726 .name = "fujitsu::eco_led",
727 .brightness_set_blocking = eco_led_set,
728 .brightness_get = eco_led_get
729};
730
Michał Kępień81f68212017-04-07 15:07:11 +0200731static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930732{
Michał Kępień30943e12017-04-07 15:07:12 +0200733 int result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200734
735 if (call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
Michał Kępień81f68212017-04-07 15:07:11 +0200736 result = devm_led_classdev_register(&device->dev,
737 &logolamp_led);
738 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200739 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200740 }
741
742 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
743 (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200744 result = devm_led_classdev_register(&device->dev, &kblamps_led);
745 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200746 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200747 }
748
749 /*
750 * BTNI bit 24 seems to indicate the presence of a radio toggle
751 * button in place of a slide switch, and all such machines appear
752 * to also have an RF LED. Therefore use bit 24 as an indicator
753 * that an RF LED is present.
754 */
755 if (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) & BIT(24)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200756 result = devm_led_classdev_register(&device->dev, &radio_led);
757 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200758 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200759 }
760
761 /* Support for eco led is not always signaled in bit corresponding
762 * to the bit used to control the led. According to the DSDT table,
763 * bit 14 seems to indicate presence of said led as well.
764 * Confirm by testing the status.
765 */
766 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
767 (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200768 result = devm_led_classdev_register(&device->dev, &eco_led);
769 if (result)
Michał Kępień30943e12017-04-07 15:07:12 +0200770 return result;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200771 }
772
Michał Kępień30943e12017-04-07 15:07:12 +0200773 return 0;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200774}
775
776static int acpi_fujitsu_laptop_add(struct acpi_device *device)
777{
Jonathan Woithe20b93732008-06-11 10:14:56 +0930778 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930779 int error;
780 int i;
781
782 if (!device)
783 return -EINVAL;
784
Alan Jenkins6942eab2017-02-08 14:46:25 +0100785 fujitsu_laptop->acpi_handle = device->handle;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930786 sprintf(acpi_device_name(device), "%s",
Alan Jenkins6942eab2017-02-08 14:46:25 +0100787 ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930788 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Alan Jenkins6942eab2017-02-08 14:46:25 +0100789 device->driver_data = fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930790
Jonathan Woithe20b93732008-06-11 10:14:56 +0930791 /* kfifo */
Alan Jenkins6942eab2017-02-08 14:46:25 +0100792 spin_lock_init(&fujitsu_laptop->fifo_lock);
793 error = kfifo_alloc(&fujitsu_laptop->fifo, RINGBUFFERSIZE * sizeof(int),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800794 GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800795 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700796 pr_err("kfifo_alloc failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930797 goto err_stop;
798 }
799
Michał Kępień11182db2017-03-20 10:32:20 +0100800 error = acpi_fujitsu_laptop_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930801 if (error)
Michał Kępień11182db2017-03-20 10:32:20 +0100802 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930803
Alan Jenkins6942eab2017-02-08 14:46:25 +0100804 error = acpi_bus_update_power(fujitsu_laptop->acpi_handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100805 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700806 pr_err("Error reading power state\n");
Michał Kępieńf66735f2017-03-20 10:32:21 +0100807 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930808 }
809
Joe Perches77bad7c2011-03-29 15:21:39 -0700810 pr_info("ACPI: %s [%s] (%s)\n",
811 acpi_device_name(device), acpi_device_bid(device),
812 !device->power.state ? "on" : "off");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930813
Alan Jenkins6942eab2017-02-08 14:46:25 +0100814 fujitsu_laptop->dev = device;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930815
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800816 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930817 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
818 if (ACPI_FAILURE
819 (acpi_evaluate_object
820 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700821 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930822 }
823
Tony Vroon3a407082008-12-31 18:19:59 +0000824 i = 0;
825 while (call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0
826 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE)
827 ; /* No action, result is discarded */
Jonathan Woithe20b93732008-06-11 10:14:56 +0930828 vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
829
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100830 fujitsu_laptop->flags_supported =
831 call_fext_func(FUNC_FLAGS, 0x0, 0x0, 0x0);
Tony Vroon4898c2b2009-02-02 11:11:10 +0000832
833 /* Make sure our bitmask of supported functions is cleared if the
834 RFKILL function block is not implemented, like on the S7020. */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100835 if (fujitsu_laptop->flags_supported == UNSUPPORTED_CMD)
836 fujitsu_laptop->flags_supported = 0;
Tony Vroon4898c2b2009-02-02 11:11:10 +0000837
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100838 if (fujitsu_laptop->flags_supported)
839 fujitsu_laptop->flags_state =
840 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
Tony Vroon3a407082008-12-31 18:19:59 +0000841
842 /* Suspect this is a keymap of the application panel, print it */
Joe Perches77bad7c2011-03-29 15:21:39 -0700843 pr_info("BTNI: [0x%x]\n", call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0));
Tony Vroon3a407082008-12-31 18:19:59 +0000844
Michał Kępień1877e262017-03-10 11:50:34 +0100845 /* Sync backlight power status */
Michał Kępień679374e2017-05-19 09:44:42 +0200846 if (fujitsu_bl && fujitsu_bl->bl_device &&
Michał Kępieńaea31372017-03-10 11:50:35 +0100847 acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Michał Kępień1877e262017-03-10 11:50:34 +0100848 if (call_fext_func(FUNC_BACKLIGHT, 0x2, 0x4, 0x0) == 3)
849 fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
850 else
851 fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
852 }
853
Michał Kępieńd1c70732017-04-07 15:07:13 +0200854 error = acpi_fujitsu_laptop_leds_register(device);
855 if (error)
856 goto err_free_fifo;
857
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100858 error = fujitsu_laptop_platform_add();
859 if (error)
Michał Kępieńf66735f2017-03-20 10:32:21 +0100860 goto err_free_fifo;
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100861
Michał Kępień7adb7b12017-04-07 15:07:09 +0200862 return 0;
Michał Kępień4f625682016-04-12 22:06:34 +0930863
Bjorn Helgaasb4ec0272009-04-07 15:37:22 +0000864err_free_fifo:
Alan Jenkins6942eab2017-02-08 14:46:25 +0100865 kfifo_free(&fujitsu_laptop->fifo);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930866err_stop:
Julia Lawallb30bb892013-12-29 23:47:36 +0100867 return error;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930868}
869
Alan Jenkins6942eab2017-02-08 14:46:25 +0100870static int acpi_fujitsu_laptop_remove(struct acpi_device *device)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930871{
Michał Kępień7ec3b542017-05-19 09:44:41 +0200872 struct fujitsu_laptop *priv = acpi_driver_data(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930873
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100874 fujitsu_laptop_platform_remove();
875
Michał Kępień7ec3b542017-05-19 09:44:41 +0200876 kfifo_free(&priv->fifo);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930877
878 return 0;
879}
880
Michał Kępień527483a2017-03-20 10:32:22 +0100881static void acpi_fujitsu_laptop_press(int scancode)
Michał Kępień2451d192017-01-11 09:59:31 +0100882{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100883 struct input_dev *input = fujitsu_laptop->input;
Michał Kępień2451d192017-01-11 09:59:31 +0100884 int status;
885
Alan Jenkins6942eab2017-02-08 14:46:25 +0100886 status = kfifo_in_locked(&fujitsu_laptop->fifo,
Michał Kępień527483a2017-03-20 10:32:22 +0100887 (unsigned char *)&scancode, sizeof(scancode),
Alan Jenkins6942eab2017-02-08 14:46:25 +0100888 &fujitsu_laptop->fifo_lock);
Michał Kępień527483a2017-03-20 10:32:22 +0100889 if (status != sizeof(scancode)) {
Michał Kępień2451d192017-01-11 09:59:31 +0100890 vdbg_printk(FUJLAPTOP_DBG_WARN,
Michał Kępień527483a2017-03-20 10:32:22 +0100891 "Could not push scancode [0x%x]\n", scancode);
Michał Kępieńa28c7e92017-01-11 09:59:33 +0100892 return;
Michał Kępień2451d192017-01-11 09:59:31 +0100893 }
Michał Kępień527483a2017-03-20 10:32:22 +0100894 sparse_keymap_report_event(input, scancode, 1, false);
Michał Kępieńa28c7e92017-01-11 09:59:33 +0100895 vdbg_printk(FUJLAPTOP_DBG_TRACE,
Michał Kępień527483a2017-03-20 10:32:22 +0100896 "Push scancode into ringbuffer [0x%x]\n", scancode);
Michał Kępień2451d192017-01-11 09:59:31 +0100897}
898
Alan Jenkins6942eab2017-02-08 14:46:25 +0100899static void acpi_fujitsu_laptop_release(void)
Michał Kępień2451d192017-01-11 09:59:31 +0100900{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100901 struct input_dev *input = fujitsu_laptop->input;
Michał Kępień527483a2017-03-20 10:32:22 +0100902 int scancode, status;
Michał Kępień2451d192017-01-11 09:59:31 +0100903
Michał Kępień29544f02017-01-11 09:59:32 +0100904 while (true) {
Alan Jenkins6942eab2017-02-08 14:46:25 +0100905 status = kfifo_out_locked(&fujitsu_laptop->fifo,
Michał Kępień527483a2017-03-20 10:32:22 +0100906 (unsigned char *)&scancode,
907 sizeof(scancode),
Alan Jenkins6942eab2017-02-08 14:46:25 +0100908 &fujitsu_laptop->fifo_lock);
Michał Kępień527483a2017-03-20 10:32:22 +0100909 if (status != sizeof(scancode))
Michał Kępień29544f02017-01-11 09:59:32 +0100910 return;
Michał Kępień527483a2017-03-20 10:32:22 +0100911 sparse_keymap_report_event(input, scancode, 0, false);
Michał Kępień2451d192017-01-11 09:59:31 +0100912 vdbg_printk(FUJLAPTOP_DBG_TRACE,
Michał Kępień527483a2017-03-20 10:32:22 +0100913 "Pop scancode from ringbuffer [0x%x]\n", scancode);
Michał Kępień2451d192017-01-11 09:59:31 +0100914 }
915}
916
Alan Jenkins6942eab2017-02-08 14:46:25 +0100917static void acpi_fujitsu_laptop_notify(struct acpi_device *device, u32 event)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930918{
919 struct input_dev *input;
Michał Kępień527483a2017-03-20 10:32:22 +0100920 int scancode, i = 0;
921 unsigned int irb;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930922
Alan Jenkins6942eab2017-02-08 14:46:25 +0100923 input = fujitsu_laptop->input;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930924
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100925 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930926 vdbg_printk(FUJLAPTOP_DBG_WARN,
927 "Unsupported event [0x%x]\n", event);
Michał Kępień527483a2017-03-20 10:32:22 +0100928 sparse_keymap_report_event(input, -1, 1, true);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100929 return;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930930 }
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100931
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100932 if (fujitsu_laptop->flags_supported)
933 fujitsu_laptop->flags_state =
934 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100935
Michał Kępień527483a2017-03-20 10:32:22 +0100936 while ((irb = call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0)) != 0 &&
937 i++ < MAX_HOTKEY_RINGBUFFER_SIZE) {
938 scancode = irb & 0x4ff;
939 if (sparse_keymap_entry_from_scancode(input, scancode))
940 acpi_fujitsu_laptop_press(scancode);
941 else if (scancode == 0)
942 acpi_fujitsu_laptop_release();
943 else
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100944 vdbg_printk(FUJLAPTOP_DBG_WARN,
945 "Unknown GIRB result [%x]\n", irb);
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100946 }
947
948 /* On some models (first seen on the Skylake-based Lifebook
949 * E736/E746/E756), the touchpad toggle hotkey (Fn+F4) is
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100950 * handled in software; its state is queried using FUNC_FLAGS
Michał Kępieńeb357cb2017-01-11 09:59:30 +0100951 */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100952 if ((fujitsu_laptop->flags_supported & BIT(26)) &&
Michał Kępień527483a2017-03-20 10:32:22 +0100953 (call_fext_func(FUNC_FLAGS, 0x1, 0x0, 0x0) & BIT(26)))
954 sparse_keymap_report_event(input, BIT(26), 1, true);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930955}
956
957/* Initialization */
958
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100959static const struct acpi_device_id fujitsu_bl_device_ids[] = {
960 {ACPI_FUJITSU_BL_HID, 0},
Jonathan Woithed0482532007-08-29 15:58:19 +0930961 {"", 0},
962};
963
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100964static struct acpi_driver acpi_fujitsu_bl_driver = {
965 .name = ACPI_FUJITSU_BL_DRIVER_NAME,
Jonathan Woithed0482532007-08-29 15:58:19 +0930966 .class = ACPI_FUJITSU_CLASS,
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100967 .ids = fujitsu_bl_device_ids,
Jonathan Woithed0482532007-08-29 15:58:19 +0930968 .ops = {
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100969 .add = acpi_fujitsu_bl_add,
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100970 .notify = acpi_fujitsu_bl_notify,
Jonathan Woithed0482532007-08-29 15:58:19 +0930971 },
972};
973
Alan Jenkins6942eab2017-02-08 14:46:25 +0100974static const struct acpi_device_id fujitsu_laptop_device_ids[] = {
975 {ACPI_FUJITSU_LAPTOP_HID, 0},
Jonathan Woithe20b93732008-06-11 10:14:56 +0930976 {"", 0},
977};
978
Alan Jenkins6942eab2017-02-08 14:46:25 +0100979static struct acpi_driver acpi_fujitsu_laptop_driver = {
980 .name = ACPI_FUJITSU_LAPTOP_DRIVER_NAME,
Jonathan Woithe20b93732008-06-11 10:14:56 +0930981 .class = ACPI_FUJITSU_CLASS,
Alan Jenkins6942eab2017-02-08 14:46:25 +0100982 .ids = fujitsu_laptop_device_ids,
Jonathan Woithe20b93732008-06-11 10:14:56 +0930983 .ops = {
Alan Jenkins6942eab2017-02-08 14:46:25 +0100984 .add = acpi_fujitsu_laptop_add,
985 .remove = acpi_fujitsu_laptop_remove,
986 .notify = acpi_fujitsu_laptop_notify,
Jonathan Woithe20b93732008-06-11 10:14:56 +0930987 },
988};
Jonathan Woithed0482532007-08-29 15:58:19 +0930989
Zhang Rui49901412014-09-09 00:21:59 +0200990static const struct acpi_device_id fujitsu_ids[] __used = {
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100991 {ACPI_FUJITSU_BL_HID, 0},
Alan Jenkins6942eab2017-02-08 14:46:25 +0100992 {ACPI_FUJITSU_LAPTOP_HID, 0},
Zhang Rui49901412014-09-09 00:21:59 +0200993 {"", 0}
994};
995MODULE_DEVICE_TABLE(acpi, fujitsu_ids);
996
Jonathan Woithed0482532007-08-29 15:58:19 +0930997static int __init fujitsu_init(void)
998{
Michał Kępieńb8d69c12017-03-10 11:50:33 +0100999 int ret;
Jonathan Woithed0482532007-08-29 15:58:19 +09301000
1001 if (acpi_disabled)
1002 return -ENODEV;
1003
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001004 ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
1005 if (ret)
Michał Kępień679374e2017-05-19 09:44:42 +02001006 return ret;
Jonathan Woithed0482532007-08-29 15:58:19 +09301007
Jonathan Woithed0482532007-08-29 15:58:19 +09301008 /* Register platform stuff */
1009
Alan Jenkins16506022017-02-08 14:46:26 +01001010 ret = platform_driver_register(&fujitsu_pf_driver);
Jonathan Woithe20b93732008-06-11 10:14:56 +09301011 if (ret)
Michał Kępieńc33f4c02017-03-14 11:26:30 +01001012 goto err_unregister_acpi;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301013
Alan Jenkins6942eab2017-02-08 14:46:25 +01001014 /* Register laptop driver */
Jonathan Woithe20b93732008-06-11 10:14:56 +09301015
Alan Jenkins6942eab2017-02-08 14:46:25 +01001016 fujitsu_laptop = kzalloc(sizeof(struct fujitsu_laptop), GFP_KERNEL);
1017 if (!fujitsu_laptop) {
Jonathan Woithe20b93732008-06-11 10:14:56 +09301018 ret = -ENOMEM;
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001019 goto err_unregister_platform_driver;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301020 }
Jonathan Woithe20b93732008-06-11 10:14:56 +09301021
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001022 ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
1023 if (ret)
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001024 goto err_free_fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301025
Joe Perches77bad7c2011-03-29 15:21:39 -07001026 pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301027
1028 return 0;
1029
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001030err_free_fujitsu_laptop:
Alan Jenkins6942eab2017-02-08 14:46:25 +01001031 kfree(fujitsu_laptop);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001032err_unregister_platform_driver:
Alan Jenkins16506022017-02-08 14:46:26 +01001033 platform_driver_unregister(&fujitsu_pf_driver);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001034err_unregister_acpi:
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001035 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Jonathan Woithed0482532007-08-29 15:58:19 +09301036
1037 return ret;
1038}
1039
1040static void __exit fujitsu_cleanup(void)
1041{
Alan Jenkins6942eab2017-02-08 14:46:25 +01001042 acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
Tony Vroon3a407082008-12-31 18:19:59 +00001043
Alan Jenkins6942eab2017-02-08 14:46:25 +01001044 kfree(fujitsu_laptop);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301045
Alan Jenkins16506022017-02-08 14:46:26 +01001046 platform_driver_unregister(&fujitsu_pf_driver);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301047
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001048 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Jonathan Woithed0482532007-08-29 15:58:19 +09301049
Joe Perches77bad7c2011-03-29 15:21:39 -07001050 pr_info("driver unloaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301051}
1052
1053module_init(fujitsu_init);
1054module_exit(fujitsu_cleanup);
1055
Michał Kępieńe06e4832017-04-05 08:49:05 +02001056module_param(use_alt_lcd_levels, int, 0644);
1057MODULE_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 +02001058module_param(disable_brightness_adjust, bool, 0644);
1059MODULE_PARM_DESC(disable_brightness_adjust, "Disable LCD brightness adjustment");
Jonathan Woithe20b93732008-06-11 10:14:56 +09301060#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
1061module_param_named(debug, dbg_level, uint, 0644);
1062MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
1063#endif
1064
Tony Vroon3a407082008-12-31 18:19:59 +00001065MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
Jonathan Woithed0482532007-08-29 15:58:19 +09301066MODULE_DESCRIPTION("Fujitsu laptop extras support");
1067MODULE_VERSION(FUJITSU_DRIVER_VERSION);
1068MODULE_LICENSE("GPL");