blob: c3e8df425e23be2c421915d41f290b49cfe29175 [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{
351 struct fujitsu_bl *fujitsu_bl = 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ńf8a399d2017-03-20 10:32:18 +0100354 fujitsu_bl->input = devm_input_allocate_device(&device->dev);
355 if (!fujitsu_bl->input)
Michał Kępień7d134e42017-03-20 10:32:17 +0100356 return -ENOMEM;
357
358 snprintf(fujitsu_bl->phys, sizeof(fujitsu_bl->phys),
359 "%s/video/input0", acpi_device_hid(device));
360
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100361 fujitsu_bl->input->name = acpi_device_name(device);
362 fujitsu_bl->input->phys = fujitsu_bl->phys;
363 fujitsu_bl->input->id.bustype = BUS_HOST;
364 fujitsu_bl->input->id.product = 0x06;
Michał Kępieńf2252672017-03-20 10:32:19 +0100365
366 ret = sparse_keymap_setup(fujitsu_bl->input, keymap_backlight, NULL);
367 if (ret)
368 return ret;
Michał Kępień7d134e42017-03-20 10:32:17 +0100369
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100370 return input_register_device(fujitsu_bl->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{
Jonathan Woithed0482532007-08-29 15:58:19 +0930395 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930396 int error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930397
Michał Kępień07acf622017-04-05 08:49:00 +0200398 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
399 return -ENODEV;
400
Jonathan Woithed0482532007-08-29 15:58:19 +0930401 if (!device)
402 return -EINVAL;
403
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100404 fujitsu_bl->acpi_handle = device->handle;
405 sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_BL_DEVICE_NAME);
Jonathan Woithed0482532007-08-29 15:58:19 +0930406 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100407 device->driver_data = fujitsu_bl;
Jonathan Woithed0482532007-08-29 15:58:19 +0930408
Michał Kępień7d134e42017-03-20 10:32:17 +0100409 error = acpi_fujitsu_bl_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930410 if (error)
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100411 return error;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930412
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100413 error = acpi_bus_update_power(fujitsu_bl->acpi_handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100414 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700415 pr_err("Error reading power state\n");
Michał Kępieńf8a399d2017-03-20 10:32:18 +0100416 return error;
Jonathan Woithed0482532007-08-29 15:58:19 +0930417 }
418
Joe Perches77bad7c2011-03-29 15:21:39 -0700419 pr_info("ACPI: %s [%s] (%s)\n",
Jonathan Woithed0482532007-08-29 15:58:19 +0930420 acpi_device_name(device), acpi_device_bid(device),
421 !device->power.state ? "on" : "off");
422
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800423 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930424 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
425 if (ACPI_FAILURE
426 (acpi_evaluate_object
427 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700428 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930429 }
430
Jonathan Woithe20b93732008-06-11 10:14:56 +0930431 if (get_max_brightness() <= 0)
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100432 fujitsu_bl->max_brightness = FUJITSU_LCD_N_LEVELS;
Tony Vroonf87a1a52009-01-07 10:11:24 +0000433 get_lcd_level();
Jonathan Woithe20b93732008-06-11 10:14:56 +0930434
Michał Kępieńa1aabd52017-04-05 08:49:01 +0200435 error = fujitsu_backlight_register(device);
Michał Kępień07acf622017-04-05 08:49:00 +0200436 if (error)
437 return error;
Michał Kępieńaea31372017-03-10 11:50:35 +0100438
Julia Lawallb30bb892013-12-29 23:47:36 +0100439 return 0;
Jonathan Woithed0482532007-08-29 15:58:19 +0930440}
441
Jonathan Woithe20b93732008-06-11 10:14:56 +0930442/* Brightness notify */
443
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100444static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930445{
446 struct input_dev *input;
Michał Kępieńf2252672017-03-20 10:32:19 +0100447 int oldb, newb;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930448
Alan Jenkins9fc5cf62017-02-08 14:46:24 +0100449 input = fujitsu_bl->input;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930450
Michał Kępień5efc8002017-03-01 07:42:53 +0100451 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930452 vdbg_printk(FUJLAPTOP_DBG_WARN,
453 "unsupported event [0x%x]\n", event);
Michał Kępieńf2252672017-03-20 10:32:19 +0100454 sparse_keymap_report_event(input, -1, 1, true);
Michał Kępień5efc8002017-03-01 07:42:53 +0100455 return;
456 }
457
Michał Kępień5efc8002017-03-01 07:42:53 +0100458 oldb = fujitsu_bl->brightness_level;
459 get_lcd_level();
460 newb = fujitsu_bl->brightness_level;
461
Michał Kępień5c495d62017-04-05 08:49:10 +0200462 vdbg_printk(FUJLAPTOP_DBG_TRACE, "brightness button event [%i -> %i]\n",
463 oldb, newb);
Michał Kępień5efc8002017-03-01 07:42:53 +0100464
Michał Kępieńd2aa3ae2017-03-01 07:42:54 +0100465 if (oldb == newb)
466 return;
467
Michał Kępieńb4bb0cf2017-04-05 08:49:06 +0200468 if (!disable_brightness_adjust)
Michał Kępieńe32c50b2017-04-05 08:49:02 +0200469 set_lcd_level(newb);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930470
Michał Kępieńf2252672017-03-20 10:32:19 +0100471 sparse_keymap_report_event(input, oldb < newb, 1, true);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930472}
473
474/* ACPI device for hotkey handling */
475
Michał Kępień527483a2017-03-20 10:32:22 +0100476static const struct key_entry keymap_default[] = {
477 { KE_KEY, KEY1_CODE, { KEY_PROG1 } },
478 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
479 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
480 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
481 { KE_KEY, KEY5_CODE, { KEY_RFKILL } },
482 { KE_KEY, BIT(26), { KEY_TOUCHPAD_TOGGLE } },
483 { KE_END, 0 }
484};
485
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100486static const struct key_entry keymap_s64x0[] = {
487 { KE_KEY, KEY1_CODE, { KEY_SCREENLOCK } }, /* "Lock" */
488 { KE_KEY, KEY2_CODE, { KEY_HELP } }, /* "Mobility Center */
489 { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
490 { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
491 { KE_END, 0 }
492};
493
494static const struct key_entry keymap_p8010[] = {
495 { KE_KEY, KEY1_CODE, { KEY_HELP } }, /* "Support" */
496 { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
497 { KE_KEY, KEY3_CODE, { KEY_SWITCHVIDEOMODE } }, /* "Presentation" */
498 { KE_KEY, KEY4_CODE, { KEY_WWW } }, /* "WWW" */
499 { KE_END, 0 }
500};
501
Michał Kępień527483a2017-03-20 10:32:22 +0100502static const struct key_entry *keymap = keymap_default;
503
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100504static int fujitsu_laptop_dmi_keymap_override(const struct dmi_system_id *id)
505{
506 pr_info("Identified laptop model '%s'\n", id->ident);
507 keymap = id->driver_data;
508 return 1;
509}
510
511static const struct dmi_system_id fujitsu_laptop_dmi_table[] = {
512 {
513 .callback = fujitsu_laptop_dmi_keymap_override,
514 .ident = "Fujitsu Siemens S6410",
515 .matches = {
516 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
517 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
518 },
519 .driver_data = (void *)keymap_s64x0
520 },
521 {
522 .callback = fujitsu_laptop_dmi_keymap_override,
523 .ident = "Fujitsu Siemens S6420",
524 .matches = {
525 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
526 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6420"),
527 },
528 .driver_data = (void *)keymap_s64x0
529 },
530 {
531 .callback = fujitsu_laptop_dmi_keymap_override,
532 .ident = "Fujitsu LifeBook P8010",
533 .matches = {
534 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
535 DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
536 },
537 .driver_data = (void *)keymap_p8010
538 },
539 {}
540};
541
Michał Kępień11182db2017-03-20 10:32:20 +0100542static int acpi_fujitsu_laptop_input_setup(struct acpi_device *device)
543{
544 struct fujitsu_laptop *fujitsu_laptop = acpi_driver_data(device);
Michał Kępień527483a2017-03-20 10:32:22 +0100545 int ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100546
Michał Kępieńf66735f2017-03-20 10:32:21 +0100547 fujitsu_laptop->input = devm_input_allocate_device(&device->dev);
548 if (!fujitsu_laptop->input)
Michał Kępień11182db2017-03-20 10:32:20 +0100549 return -ENOMEM;
550
551 snprintf(fujitsu_laptop->phys, sizeof(fujitsu_laptop->phys),
552 "%s/video/input0", acpi_device_hid(device));
553
Michał Kępieńf66735f2017-03-20 10:32:21 +0100554 fujitsu_laptop->input->name = acpi_device_name(device);
555 fujitsu_laptop->input->phys = fujitsu_laptop->phys;
556 fujitsu_laptop->input->id.bustype = BUS_HOST;
557 fujitsu_laptop->input->id.product = 0x06;
Michał Kępień11182db2017-03-20 10:32:20 +0100558
Michał Kępieńf8c94ec2017-03-20 10:32:23 +0100559 dmi_check_system(fujitsu_laptop_dmi_table);
Michał Kępień527483a2017-03-20 10:32:22 +0100560 ret = sparse_keymap_setup(fujitsu_laptop->input, keymap, NULL);
561 if (ret)
562 return ret;
Michał Kępień11182db2017-03-20 10:32:20 +0100563
Michał Kępieńf66735f2017-03-20 10:32:21 +0100564 return input_register_device(fujitsu_laptop->input);
Michał Kępień11182db2017-03-20 10:32:20 +0100565}
566
Michał Kępieńd811b512017-03-14 11:26:29 +0100567static int fujitsu_laptop_platform_add(void)
568{
569 int ret;
570
Michał Kępień979800e2017-03-14 11:26:31 +0100571 fujitsu_laptop->pf_device = platform_device_alloc("fujitsu-laptop", -1);
572 if (!fujitsu_laptop->pf_device)
Michał Kępieńd811b512017-03-14 11:26:29 +0100573 return -ENOMEM;
574
Michał Kępień979800e2017-03-14 11:26:31 +0100575 ret = platform_device_add(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100576 if (ret)
577 goto err_put_platform_device;
578
Michał Kępień979800e2017-03-14 11:26:31 +0100579 ret = sysfs_create_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100580 &fujitsu_pf_attribute_group);
581 if (ret)
582 goto err_del_platform_device;
583
584 return 0;
585
586err_del_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100587 platform_device_del(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100588err_put_platform_device:
Michał Kępień979800e2017-03-14 11:26:31 +0100589 platform_device_put(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100590
591 return ret;
592}
593
594static void fujitsu_laptop_platform_remove(void)
595{
Michał Kępień979800e2017-03-14 11:26:31 +0100596 sysfs_remove_group(&fujitsu_laptop->pf_device->dev.kobj,
Michał Kępieńd811b512017-03-14 11:26:29 +0100597 &fujitsu_pf_attribute_group);
Michał Kępień979800e2017-03-14 11:26:31 +0100598 platform_device_unregister(fujitsu_laptop->pf_device);
Michał Kępieńd811b512017-03-14 11:26:29 +0100599}
600
Michał Kępieńe33ca452017-04-07 15:07:10 +0200601static int logolamp_set(struct led_classdev *cdev,
602 enum led_brightness brightness)
603{
604 int poweron = FUNC_LED_ON, always = FUNC_LED_ON;
605 int ret;
606
607 if (brightness < LED_HALF)
608 poweron = FUNC_LED_OFF;
609
610 if (brightness < LED_FULL)
611 always = FUNC_LED_OFF;
612
613 ret = call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_POWERON, poweron);
614 if (ret < 0)
615 return ret;
616
617 return call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_ALWAYS, always);
618}
619
620static enum led_brightness logolamp_get(struct led_classdev *cdev)
621{
622 int ret;
623
624 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_ALWAYS, 0x0);
625 if (ret == FUNC_LED_ON)
626 return LED_FULL;
627
628 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_POWERON, 0x0);
629 if (ret == FUNC_LED_ON)
630 return LED_HALF;
631
632 return LED_OFF;
633}
634
635static struct led_classdev logolamp_led = {
636 .name = "fujitsu::logolamp",
637 .brightness_set_blocking = logolamp_set,
638 .brightness_get = logolamp_get
639};
640
641static int kblamps_set(struct led_classdev *cdev,
642 enum led_brightness brightness)
643{
644 if (brightness >= LED_FULL)
645 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
646 FUNC_LED_ON);
647 else
648 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
649 FUNC_LED_OFF);
650}
651
652static enum led_brightness kblamps_get(struct led_classdev *cdev)
653{
654 enum led_brightness brightness = LED_OFF;
655
656 if (call_fext_func(FUNC_LEDS, 0x2, KEYBOARD_LAMPS, 0x0) == FUNC_LED_ON)
657 brightness = LED_FULL;
658
659 return brightness;
660}
661
662static struct led_classdev kblamps_led = {
663 .name = "fujitsu::kblamps",
664 .brightness_set_blocking = kblamps_set,
665 .brightness_get = kblamps_get
666};
667
668static int radio_led_set(struct led_classdev *cdev,
669 enum led_brightness brightness)
670{
671 if (brightness >= LED_FULL)
672 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON,
673 RADIO_LED_ON);
674 else
675 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON, 0x0);
676}
677
678static enum led_brightness radio_led_get(struct led_classdev *cdev)
679{
680 enum led_brightness brightness = LED_OFF;
681
682 if (call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0) & RADIO_LED_ON)
683 brightness = LED_FULL;
684
685 return brightness;
686}
687
688static struct led_classdev radio_led = {
689 .name = "fujitsu::radio_led",
690 .brightness_set_blocking = radio_led_set,
691 .brightness_get = radio_led_get,
692 .default_trigger = "rfkill-any"
693};
694
695static int eco_led_set(struct led_classdev *cdev,
696 enum led_brightness brightness)
697{
698 int curr;
699
700 curr = call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0);
701 if (brightness >= LED_FULL)
702 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
703 curr | ECO_LED_ON);
704 else
705 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED,
706 curr & ~ECO_LED_ON);
707}
708
709static enum led_brightness eco_led_get(struct led_classdev *cdev)
710{
711 enum led_brightness brightness = LED_OFF;
712
713 if (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) & ECO_LED_ON)
714 brightness = LED_FULL;
715
716 return brightness;
717}
718
719static struct led_classdev eco_led = {
720 .name = "fujitsu::eco_led",
721 .brightness_set_blocking = eco_led_set,
722 .brightness_get = eco_led_get
723};
724
Michał Kępień81f68212017-04-07 15:07:11 +0200725static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device)
Jonathan Woithe20b93732008-06-11 10:14:56 +0930726{
Jonathan Woithe20b93732008-06-11 10:14:56 +0930727 int result = 0;
Michał Kępień7adb7b12017-04-07 15:07:09 +0200728
729 if (call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
Michał Kępień81f68212017-04-07 15:07:11 +0200730 result = devm_led_classdev_register(&device->dev,
731 &logolamp_led);
732 if (result)
Michał Kępień7adb7b12017-04-07 15:07:09 +0200733 pr_err("Could not register LED handler for logo lamp, error %i\n",
734 result);
Michał Kępień7adb7b12017-04-07 15:07:09 +0200735 }
736
737 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
738 (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200739 result = devm_led_classdev_register(&device->dev, &kblamps_led);
740 if (result)
Michał Kępień7adb7b12017-04-07 15:07:09 +0200741 pr_err("Could not register LED handler for keyboard lamps, error %i\n",
742 result);
Michał Kępień7adb7b12017-04-07 15:07:09 +0200743 }
744
745 /*
746 * BTNI bit 24 seems to indicate the presence of a radio toggle
747 * button in place of a slide switch, and all such machines appear
748 * to also have an RF LED. Therefore use bit 24 as an indicator
749 * that an RF LED is present.
750 */
751 if (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) & BIT(24)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200752 result = devm_led_classdev_register(&device->dev, &radio_led);
753 if (result)
Michał Kępień7adb7b12017-04-07 15:07:09 +0200754 pr_err("Could not register LED handler for radio LED, error %i\n",
755 result);
Michał Kępień7adb7b12017-04-07 15:07:09 +0200756 }
757
758 /* Support for eco led is not always signaled in bit corresponding
759 * to the bit used to control the led. According to the DSDT table,
760 * bit 14 seems to indicate presence of said led as well.
761 * Confirm by testing the status.
762 */
763 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
764 (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
Michał Kępień81f68212017-04-07 15:07:11 +0200765 result = devm_led_classdev_register(&device->dev, &eco_led);
766 if (result)
Michał Kępień7adb7b12017-04-07 15:07:09 +0200767 pr_err("Could not register LED handler for eco LED, error %i\n",
768 result);
Michał Kępień7adb7b12017-04-07 15:07:09 +0200769 }
770
771 return result;
772}
773
774static int acpi_fujitsu_laptop_add(struct acpi_device *device)
775{
Jonathan Woithe20b93732008-06-11 10:14:56 +0930776 int state = 0;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930777 int error;
778 int i;
779
780 if (!device)
781 return -EINVAL;
782
Alan Jenkins6942eab2017-02-08 14:46:25 +0100783 fujitsu_laptop->acpi_handle = device->handle;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930784 sprintf(acpi_device_name(device), "%s",
Alan Jenkins6942eab2017-02-08 14:46:25 +0100785 ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930786 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
Alan Jenkins6942eab2017-02-08 14:46:25 +0100787 device->driver_data = fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930788
Jonathan Woithe20b93732008-06-11 10:14:56 +0930789 /* kfifo */
Alan Jenkins6942eab2017-02-08 14:46:25 +0100790 spin_lock_init(&fujitsu_laptop->fifo_lock);
791 error = kfifo_alloc(&fujitsu_laptop->fifo, RINGBUFFERSIZE * sizeof(int),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800792 GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800793 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700794 pr_err("kfifo_alloc failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930795 goto err_stop;
796 }
797
Michał Kępień11182db2017-03-20 10:32:20 +0100798 error = acpi_fujitsu_laptop_input_setup(device);
Jonathan Woithe20b93732008-06-11 10:14:56 +0930799 if (error)
Michał Kępień11182db2017-03-20 10:32:20 +0100800 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930801
Alan Jenkins6942eab2017-02-08 14:46:25 +0100802 error = acpi_bus_update_power(fujitsu_laptop->acpi_handle, &state);
Julia Lawallb30bb892013-12-29 23:47:36 +0100803 if (error) {
Joe Perches77bad7c2011-03-29 15:21:39 -0700804 pr_err("Error reading power state\n");
Michał Kępieńf66735f2017-03-20 10:32:21 +0100805 goto err_free_fifo;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930806 }
807
Joe Perches77bad7c2011-03-29 15:21:39 -0700808 pr_info("ACPI: %s [%s] (%s)\n",
809 acpi_device_name(device), acpi_device_bid(device),
810 !device->power.state ? "on" : "off");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930811
Alan Jenkins6942eab2017-02-08 14:46:25 +0100812 fujitsu_laptop->dev = device;
Jonathan Woithe20b93732008-06-11 10:14:56 +0930813
Zhang Ruidd13b9a2013-09-03 08:32:03 +0800814 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
Jonathan Woithe20b93732008-06-11 10:14:56 +0930815 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
816 if (ACPI_FAILURE
817 (acpi_evaluate_object
818 (device->handle, METHOD_NAME__INI, NULL, NULL)))
Joe Perches77bad7c2011-03-29 15:21:39 -0700819 pr_err("_INI Method failed\n");
Jonathan Woithe20b93732008-06-11 10:14:56 +0930820 }
821
Tony Vroon3a407082008-12-31 18:19:59 +0000822 i = 0;
823 while (call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0
824 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE)
825 ; /* No action, result is discarded */
Jonathan Woithe20b93732008-06-11 10:14:56 +0930826 vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
827
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100828 fujitsu_laptop->flags_supported =
829 call_fext_func(FUNC_FLAGS, 0x0, 0x0, 0x0);
Tony Vroon4898c2b2009-02-02 11:11:10 +0000830
831 /* Make sure our bitmask of supported functions is cleared if the
832 RFKILL function block is not implemented, like on the S7020. */
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100833 if (fujitsu_laptop->flags_supported == UNSUPPORTED_CMD)
834 fujitsu_laptop->flags_supported = 0;
Tony Vroon4898c2b2009-02-02 11:11:10 +0000835
Alan Jenkins8ef27bd2017-02-08 14:46:27 +0100836 if (fujitsu_laptop->flags_supported)
837 fujitsu_laptop->flags_state =
838 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
Tony Vroon3a407082008-12-31 18:19:59 +0000839
840 /* Suspect this is a keymap of the application panel, print it */
Joe Perches77bad7c2011-03-29 15:21:39 -0700841 pr_info("BTNI: [0x%x]\n", call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0));
Tony Vroon3a407082008-12-31 18:19:59 +0000842
Michał Kępień1877e262017-03-10 11:50:34 +0100843 /* Sync backlight power status */
Michał Kępieńaea31372017-03-10 11:50:35 +0100844 if (fujitsu_bl->bl_device &&
845 acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Michał Kępień1877e262017-03-10 11:50:34 +0100846 if (call_fext_func(FUNC_BACKLIGHT, 0x2, 0x4, 0x0) == 3)
847 fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
848 else
849 fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
850 }
851
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100852 error = fujitsu_laptop_platform_add();
853 if (error)
Michał Kępieńf66735f2017-03-20 10:32:21 +0100854 goto err_free_fifo;
Michał Kępieńc33f4c02017-03-14 11:26:30 +0100855
Michał Kępień81f68212017-04-07 15:07:11 +0200856 error = acpi_fujitsu_laptop_leds_register(device);
Michał Kępień7adb7b12017-04-07 15:07:09 +0200857 if (error)
858 goto err_remove_platform_device;
Tony Vroon3a407082008-12-31 18:19:59 +0000859
Michał Kępień7adb7b12017-04-07 15:07:09 +0200860 return 0;
Michał Kępień4f625682016-04-12 22:06:34 +0930861
Michał Kępień7adb7b12017-04-07 15:07:09 +0200862err_remove_platform_device:
863 fujitsu_laptop_platform_remove();
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{
Alan Jenkins6942eab2017-02-08 14:46:25 +0100872 struct fujitsu_laptop *fujitsu_laptop = 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
Alan Jenkins6942eab2017-02-08 14:46:25 +0100876 kfifo_free(&fujitsu_laptop->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 Jenkins9fc5cf62017-02-08 14:46:24 +01001004 fujitsu_bl = kzalloc(sizeof(struct fujitsu_bl), GFP_KERNEL);
1005 if (!fujitsu_bl)
Jonathan Woithed0482532007-08-29 15:58:19 +09301006 return -ENOMEM;
Jonathan Woithed0482532007-08-29 15:58:19 +09301007
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001008 ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
1009 if (ret)
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001010 goto err_free_fujitsu_bl;
Jonathan Woithed0482532007-08-29 15:58:19 +09301011
Jonathan Woithed0482532007-08-29 15:58:19 +09301012 /* Register platform stuff */
1013
Alan Jenkins16506022017-02-08 14:46:26 +01001014 ret = platform_driver_register(&fujitsu_pf_driver);
Jonathan Woithe20b93732008-06-11 10:14:56 +09301015 if (ret)
Michał Kępieńc33f4c02017-03-14 11:26:30 +01001016 goto err_unregister_acpi;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301017
Alan Jenkins6942eab2017-02-08 14:46:25 +01001018 /* Register laptop driver */
Jonathan Woithe20b93732008-06-11 10:14:56 +09301019
Alan Jenkins6942eab2017-02-08 14:46:25 +01001020 fujitsu_laptop = kzalloc(sizeof(struct fujitsu_laptop), GFP_KERNEL);
1021 if (!fujitsu_laptop) {
Jonathan Woithe20b93732008-06-11 10:14:56 +09301022 ret = -ENOMEM;
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001023 goto err_unregister_platform_driver;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301024 }
Jonathan Woithe20b93732008-06-11 10:14:56 +09301025
Alan Jenkinsc1d1e8a2017-02-08 14:46:30 +01001026 ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
1027 if (ret)
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001028 goto err_free_fujitsu_laptop;
Jonathan Woithe20b93732008-06-11 10:14:56 +09301029
Joe Perches77bad7c2011-03-29 15:21:39 -07001030 pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301031
1032 return 0;
1033
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001034err_free_fujitsu_laptop:
Alan Jenkins6942eab2017-02-08 14:46:25 +01001035 kfree(fujitsu_laptop);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001036err_unregister_platform_driver:
Alan Jenkins16506022017-02-08 14:46:26 +01001037 platform_driver_unregister(&fujitsu_pf_driver);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001038err_unregister_acpi:
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001039 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Michał Kępieńc2cddd42017-03-10 11:50:36 +01001040err_free_fujitsu_bl:
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001041 kfree(fujitsu_bl);
Jonathan Woithed0482532007-08-29 15:58:19 +09301042
1043 return ret;
1044}
1045
1046static void __exit fujitsu_cleanup(void)
1047{
Alan Jenkins6942eab2017-02-08 14:46:25 +01001048 acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
Tony Vroon3a407082008-12-31 18:19:59 +00001049
Alan Jenkins6942eab2017-02-08 14:46:25 +01001050 kfree(fujitsu_laptop);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301051
Alan Jenkins16506022017-02-08 14:46:26 +01001052 platform_driver_unregister(&fujitsu_pf_driver);
Bartlomiej Zolnierkiewicz72afeea2009-07-31 18:16:02 +09301053
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001054 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
Jonathan Woithed0482532007-08-29 15:58:19 +09301055
Alan Jenkins9fc5cf62017-02-08 14:46:24 +01001056 kfree(fujitsu_bl);
Jonathan Woithed0482532007-08-29 15:58:19 +09301057
Joe Perches77bad7c2011-03-29 15:21:39 -07001058 pr_info("driver unloaded\n");
Jonathan Woithed0482532007-08-29 15:58:19 +09301059}
1060
1061module_init(fujitsu_init);
1062module_exit(fujitsu_cleanup);
1063
Michał Kępieńe06e4832017-04-05 08:49:05 +02001064module_param(use_alt_lcd_levels, int, 0644);
1065MODULE_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 +02001066module_param(disable_brightness_adjust, bool, 0644);
1067MODULE_PARM_DESC(disable_brightness_adjust, "Disable LCD brightness adjustment");
Jonathan Woithe20b93732008-06-11 10:14:56 +09301068#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
1069module_param_named(debug, dbg_level, uint, 0644);
1070MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
1071#endif
1072
Tony Vroon3a407082008-12-31 18:19:59 +00001073MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
Jonathan Woithed0482532007-08-29 15:58:19 +09301074MODULE_DESCRIPTION("Fujitsu laptop extras support");
1075MODULE_VERSION(FUJITSU_DRIVER_VERSION);
1076MODULE_LICENSE("GPL");