blob: 341655d711ce95466d1bc37b18cc4fcdf4495108 [file] [log] [blame]
Thomas Gleixner16216332019-05-19 15:51:31 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03003 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007 */
8
Joe Perches0978e012011-04-04 10:06:25 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Ognjen Galic2801b962018-02-07 15:58:44 +010011#define TPACPI_VERSION "0.26"
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070012#define TPACPI_SYSFS_VERSION 0x030000
Borislav Deianov78f81cc2005-08-17 00:00:00 -040013
14/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020016 * 2007-10-20 changelog trimmed down
17 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030018 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
19 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020020 *
21 * 2006-11-22 0.13 new maintainer
22 * changelog now lives in git commit history, and will
23 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030024 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040025 * 2005-03-17 0.11 support for 600e, 770x
26 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020027 *
28 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040029 * thanks to Henrik Brix Andersen <brix@gentoo.org>
30 * fix parameter passing on module loading
31 * thanks to Rusty Russell <rusty@rustcorp.com.au>
32 * thanks to Jim Radford <radford@blackbean.org>
33 * 2004-11-08 0.8 fix init error case, don't return from a macro
34 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020037#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/string.h>
42#include <linux/list.h>
43#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000044#include <linux/sched.h>
Eric W. Biederman7a360942017-09-26 12:45:33 -050045#include <linux/sched/signal.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020046#include <linux/kthread.h>
47#include <linux/freezer.h>
48#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/nvram.h>
51#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020052#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020053#include <linux/sysfs.h>
54#include <linux/backlight.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010055#include <linux/bitops.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020056#include <linux/fb.h>
57#include <linux/platform_device.h>
58#include <linux/hwmon.h>
59#include <linux/hwmon-sysfs.h>
60#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030061#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030062#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020063#include <linux/dmi.h>
64#include <linux/jiffies.h>
65#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080066#include <linux/acpi.h>
Jiaxun Yangf7db8392019-03-07 17:37:16 +080067#include <linux/pci.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010068#include <linux/power_supply.h>
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050069#include <linux/platform_profile.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020070#include <sound/core.h>
71#include <sound/control.h>
72#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080073#include <linux/uaccess.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010074#include <acpi/battery.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020075#include <acpi/video.h>
Hans de Goedef8098912021-10-05 22:23:20 +020076#include <drm/drm_privacy_screen_driver.h>
Hans de Goede153cca92021-07-29 10:21:34 +020077#include "dual_accel_detect.h"
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020078
79/* ThinkPad CMOS commands */
80#define TP_CMOS_VOLUME_DOWN 0
81#define TP_CMOS_VOLUME_UP 1
82#define TP_CMOS_VOLUME_MUTE 2
83#define TP_CMOS_BRIGHTNESS_UP 4
84#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030085#define TP_CMOS_THINKLIGHT_ON 12
86#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087
88/* NVRAM Addresses */
89enum tp_nvram_addr {
90 TP_NVRAM_ADDR_HK2 = 0x57,
91 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
92 TP_NVRAM_ADDR_VIDEO = 0x59,
93 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
94 TP_NVRAM_ADDR_MIXER = 0x60,
95};
96
97/* NVRAM bit masks */
98enum {
99 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
100 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
101 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
102 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
103 TP_NVRAM_MASK_THINKLIGHT = 0x10,
104 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
105 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
106 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
107 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
108 TP_NVRAM_MASK_MUTE = 0x40,
109 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
110 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
111 TP_NVRAM_POS_LEVEL_VOLUME = 0,
112};
113
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300114/* Misc NVRAM-related */
115enum {
116 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400120#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
121#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800122#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300123#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200124
125/* Input IDs */
126#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
127#define TPACPI_HKEY_INPUT_VERSION 0x4101
128
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200129/* ACPI \WGSV commands */
130enum {
131 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
132 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
133 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
134 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
135};
136
137/* TP_ACPI_WGSV_GET_STATE bits */
138enum {
139 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
140 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
141 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
142 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
143 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
144 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
145 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
146 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
147 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
148 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
149};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200150
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300151/* HKEY events */
152enum tpacpi_hkey_event_t {
153 /* Hotkey-related */
154 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
155 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
156 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100157 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300158 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
159 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
160 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
Hans de Goedef8098912021-10-05 22:23:20 +0200161 TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300162
163 /* Reasons for waking up from S3/S4 */
164 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
165 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
166 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
167 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
168 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
169 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
170
171 /* Auto-sleep after eject request */
172 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
173 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
174
175 /* Misc bay events */
176 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300177 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
178 or port replicator */
179 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
180 dock or port replicator */
Alexander Kobel0a053f02021-02-13 16:13:36 +0100181 /*
182 * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
183 * when keyboard cover is attached, detached or folded onto the back
184 */
185 TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
186 TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300187
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500193 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
194 * enter/leave tablet mode
195 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300196 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
197 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
198 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
199
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300200 /* Key-related user-interface events */
201 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
202 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100203 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300204
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300205 /* Thermal events */
206 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
207 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
208 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
209 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -0300210 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
211 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
212 * command completed. Related to
213 * AML DYTC */
214 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
215 * changed. Related to AML GMTS */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300216
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100217 /* AC-related events */
218 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300219
David Herrmann587d8622018-01-12 12:04:45 +0100220 /* Further user-interface events */
221 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
222 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
223
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300224 /* Misc */
225 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
226};
227
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200228/****************************************************************************
229 * Main driver
230 */
231
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200232#define TPACPI_NAME "thinkpad"
233#define TPACPI_DESC "ThinkPad ACPI Extras"
234#define TPACPI_FILE TPACPI_NAME "_acpi"
235#define TPACPI_URL "http://ibm-acpi.sf.net/"
236#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200237
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200238#define TPACPI_PROC_DIR "ibm"
239#define TPACPI_ACPI_EVENT_PREFIX "ibm"
240#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300241#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200242#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200243
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300244#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300245#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300246
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200247#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200248
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000249/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000251#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252#define TPACPI_DBG_INIT 0x0001
253#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000254#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000255#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000256#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000257#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200258#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200259
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200260#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
261#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
262#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200263
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264
265/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200266 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200267 */
268
269struct ibm_struct;
270
271struct tp_acpi_drv_struct {
272 const struct acpi_device_id *hid;
273 struct acpi_driver *driver;
274
275 void (*notify) (struct ibm_struct *, u32);
276 acpi_handle *handle;
277 u32 type;
278 struct acpi_device *device;
279};
280
281struct ibm_struct {
282 char *name;
283
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200284 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200285 int (*write) (char *);
286 void (*exit) (void);
287 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200288 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200289 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200290
291 struct list_head all_drivers;
292
293 struct tp_acpi_drv_struct *acpi;
294
295 struct {
296 u8 acpi_driver_registered:1;
297 u8 acpi_notify_installed:1;
298 u8 proc_created:1;
299 u8 init_called:1;
300 u8 experimental:1;
301 } flags;
302};
303
304struct ibm_init_struct {
305 char param[32];
306
307 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400308 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200309 struct ibm_struct *data;
310};
311
312static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200313 u32 bluetooth:1;
314 u32 hotkey:1;
315 u32 hotkey_mask:1;
316 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500317 enum {
318 TP_HOTKEY_TABLET_NONE = 0,
319 TP_HOTKEY_TABLET_USES_MHKG,
Benjamin Bergdda3ec02017-09-15 15:20:49 +0200320 TP_HOTKEY_TABLET_USES_GMMS,
Lyudeb3180022016-11-11 15:15:02 -0500321 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100322 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323 u32 light:1;
324 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300325 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300326 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200327 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200328 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200329 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300330 u32 second_fan:1;
Lars Hofhansl14232c62020-04-23 14:57:09 -0700331 u32 second_fan_ctl:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300332 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200333 u32 mixer_no_level_control:1;
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +0200334 u32 battery_force_primary:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200335 u32 input_device_registered:1;
336 u32 platform_drv_registered:1;
337 u32 platform_drv_attrs_registered:1;
338 u32 sensors_pdrv_registered:1;
339 u32 sensors_pdrv_attrs_registered:1;
340 u32 sensors_pdev_attrs_registered:1;
341 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100342 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200343} tp_features;
344
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300345static struct {
346 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200347 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300348} tp_warned;
349
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200350struct thinkpad_id_data {
351 unsigned int vendor; /* ThinkPad vendor:
352 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
353
354 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
355 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
356
Jouke Witteveen846a4162018-07-11 11:44:41 +0200357 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
358 u32 ec_model;
359 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300360 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200361
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300362 char *model_str; /* ThinkPad T43 */
363 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200364};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200365static struct thinkpad_id_data thinkpad_id;
366
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300367static enum {
368 TPACPI_LIFE_INIT = 0,
369 TPACPI_LIFE_RUNNING,
370 TPACPI_LIFE_EXITING,
371} tpacpi_lifecycle;
372
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200373static int experimental;
374static u32 dbg_level;
375
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300376static struct workqueue_struct *tpacpi_wq;
377
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000378enum led_status_t {
379 TPACPI_LED_OFF = 0,
380 TPACPI_LED_ON,
381 TPACPI_LED_BLINK,
382};
383
Hans de Goede86ec0c22017-02-09 16:44:12 +0100384/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300385struct tpacpi_led_classdev {
386 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800387 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300388};
389
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300390/* brightness level capabilities */
391static unsigned int bright_maxlvl; /* 0 = unknown */
392
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200393#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
394static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030395static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200396static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030397static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200398static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030399static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200400static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030401static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200402#endif
403
404
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000405/*************************************************************************
406 * Debugging helpers
407 */
408
Joe Perches0978e012011-04-04 10:06:25 -0700409#define dbg_printk(a_dbg_level, format, arg...) \
410do { \
411 if (dbg_level & (a_dbg_level)) \
412 printk(KERN_DEBUG pr_fmt("%s: " format), \
413 __func__, ##arg); \
414} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000415
416#ifdef CONFIG_THINKPAD_ACPI_DEBUG
417#define vdbg_printk dbg_printk
418static const char *str_supported(int is_supported);
419#else
Joe Perches0978e012011-04-04 10:06:25 -0700420static inline const char *str_supported(int is_supported) { return ""; }
421#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700422 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000423#endif
424
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000425static void tpacpi_log_usertask(const char * const what)
426{
Joe Perches0978e012011-04-04 10:06:25 -0700427 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
428 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000429}
430
Joe Perches0978e012011-04-04 10:06:25 -0700431#define tpacpi_disclose_usertask(what, format, arg...) \
432do { \
433 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
434 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
435 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
436 what, task_tgid_vnr(current), ## arg); \
437 } \
438} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000439
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300440/*
441 * Quirk handling helpers
442 *
Jouke Witteveen846a4162018-07-11 11:44:41 +0200443 * ThinkPad IDs and versions seen in the field so far are
444 * two or three characters from the set [0-9A-Z], i.e. base 36.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300445 *
446 * We use values well outside that range as specials.
447 */
448
Jouke Witteveen846a4162018-07-11 11:44:41 +0200449#define TPACPI_MATCH_ANY 0xffffffffU
450#define TPACPI_MATCH_ANY_VERSION 0xffffU
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300451#define TPACPI_MATCH_UNKNOWN 0U
452
Jouke Witteveen846a4162018-07-11 11:44:41 +0200453/* TPID('1', 'Y') == 0x3159 */
454#define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
455#define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
456#define TPVER TPID
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300457
458#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
459 { .vendor = PCI_VENDOR_ID_IBM, \
460 .bios = TPID(__id1, __id2), \
461 .ec = TPACPI_MATCH_ANY, \
462 .quirks = (__quirk) }
463
464#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
465 { .vendor = PCI_VENDOR_ID_LENOVO, \
466 .bios = TPID(__id1, __id2), \
467 .ec = TPACPI_MATCH_ANY, \
468 .quirks = (__quirk) }
469
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +0200470#define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
471 { .vendor = PCI_VENDOR_ID_LENOVO, \
472 .bios = TPID3(__id1, __id2, __id3), \
473 .ec = TPACPI_MATCH_ANY, \
474 .quirks = (__quirk) }
475
Jouke Witteveen599eeff2018-12-05 12:17:52 +0100476#define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
477 { .vendor = PCI_VENDOR_ID_IBM, \
478 .bios = TPACPI_MATCH_ANY, \
479 .ec = TPID(__id1, __id2), \
480 .quirks = (__quirk) }
481
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200482#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
483 { .vendor = PCI_VENDOR_ID_LENOVO, \
484 .bios = TPACPI_MATCH_ANY, \
485 .ec = TPID(__id1, __id2), \
486 .quirks = (__quirk) }
487
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300488struct tpacpi_quirk {
489 unsigned int vendor;
Jouke Witteveen846a4162018-07-11 11:44:41 +0200490 u32 bios;
491 u32 ec;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300492 unsigned long quirks;
493};
494
495/**
496 * tpacpi_check_quirks() - search BIOS/EC version on a list
497 * @qlist: array of &struct tpacpi_quirk
498 * @qlist_size: number of elements in @qlist
499 *
500 * Iterates over a quirks list until one is found that matches the
501 * ThinkPad's vendor, BIOS and EC model.
502 *
503 * Returns 0 if nothing matches, otherwise returns the quirks field of
504 * the matching &struct tpacpi_quirk entry.
505 *
506 * The match criteria is: vendor, ec and bios much match.
507 */
508static unsigned long __init tpacpi_check_quirks(
509 const struct tpacpi_quirk *qlist,
510 unsigned int qlist_size)
511{
512 while (qlist_size) {
513 if ((qlist->vendor == thinkpad_id.vendor ||
514 qlist->vendor == TPACPI_MATCH_ANY) &&
515 (qlist->bios == thinkpad_id.bios_model ||
516 qlist->bios == TPACPI_MATCH_ANY) &&
517 (qlist->ec == thinkpad_id.ec_model ||
518 qlist->ec == TPACPI_MATCH_ANY))
519 return qlist->quirks;
520
521 qlist_size--;
522 qlist++;
523 }
524 return 0;
525}
526
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300527static inline bool __pure __init tpacpi_is_lenovo(void)
528{
529 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
530}
531
532static inline bool __pure __init tpacpi_is_ibm(void)
533{
534 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
535}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300536
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300537/****************************************************************************
538 ****************************************************************************
539 *
540 * ACPI Helpers and device model
541 *
542 ****************************************************************************
543 ****************************************************************************/
544
545/*************************************************************************
546 * ACPI basic handles
547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300549static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300550static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200552#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700554 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300555 &parent##_handle; \
556 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200558TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
559TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200561TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
562 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400563 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
564 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300565 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400566
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200567TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400568 "^HKEY", /* R30, R31 */
569 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400571
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300572/*************************************************************************
573 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200574 */
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700577 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400580 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200581 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400582 struct acpi_buffer result, *resultp;
583 union acpi_object out_obj;
584 acpi_status status;
585 va_list ap;
586 char res_type;
587 int success;
588 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700591 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593 }
594
595 if (*fmt == 'q') {
596 quiet = 1;
597 fmt++;
598 } else
599 quiet = 0;
600
601 res_type = *(fmt++);
602
603 params.count = 0;
604 params.pointer = &in_objs[0];
605
606 va_start(ap, fmt);
607 while (*fmt) {
608 char c = *(fmt++);
609 switch (c) {
610 case 'd': /* int */
611 in_objs[params.count].integer.value = va_arg(ap, int);
612 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
613 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400614 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300616 pr_err("acpi_evalf() called with invalid format character '%c'\n",
617 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100618 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
620 }
621 }
622 va_end(ap);
623
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400624 if (res_type != 'v') {
625 result.length = sizeof(out_obj);
626 result.pointer = &out_obj;
627 resultp = &result;
628 } else
629 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400631 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400634 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300635 success = (status == AE_OK &&
636 out_obj.type == ACPI_TYPE_INTEGER);
637 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700638 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400640 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 success = status == AE_OK;
642 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400643 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300645 pr_err("acpi_evalf() called with invalid format character '%c'\n",
646 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648 }
649
650 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700651 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300652 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 return success;
655}
656
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200657static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300658{
659 int v;
660
661 if (ecrd_handle) {
662 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
663 return 0;
664 *p = v;
665 } else {
666 if (ec_read(i, p) < 0)
667 return 0;
668 }
669
670 return 1;
671}
672
673static int acpi_ec_write(int i, u8 v)
674{
675 if (ecwr_handle) {
676 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
677 return 0;
678 } else {
679 if (ec_write(i, v) < 0)
680 return 0;
681 }
682
683 return 1;
684}
685
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300686static int issue_thinkpad_cmos_command(int cmos_cmd)
687{
688 if (!cmos_handle)
689 return -ENXIO;
690
691 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
692 return -EIO;
693
694 return 0;
695}
696
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300697/*************************************************************************
698 * ACPI device model
699 */
700
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200701#define TPACPI_ACPIHANDLE_INIT(object) \
702 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300703 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200704
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300705static void __init drv_acpi_handle_init(const char *name,
706 acpi_handle *handle, const acpi_handle parent,
707 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300708{
709 int i;
710 acpi_status status;
711
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300712 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
713 name);
714
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300715 for (i = 0; i < num_paths; i++) {
716 status = acpi_get_handle(parent, paths[i], handle);
717 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300718 dbg_printk(TPACPI_DBG_INIT,
719 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300720 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300721 return;
722 }
723 }
724
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300725 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
726 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300727 *handle = NULL;
728}
729
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300730static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
731 u32 level, void *context, void **return_value)
732{
Aaron Lu46445b62013-10-11 21:27:46 +0800733 struct acpi_device *dev;
734 if (!strcmp(context, "video")) {
735 if (acpi_bus_get_device(handle, &dev))
736 return AE_OK;
737 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
738 return AE_OK;
739 }
740
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300741 *(acpi_handle *)return_value = handle;
742
743 return AE_CTRL_TERMINATE;
744}
745
746static void __init tpacpi_acpi_handle_locate(const char *name,
747 const char *hid,
748 acpi_handle *handle)
749{
750 acpi_status status;
751 acpi_handle device_found;
752
Aaron Lu46445b62013-10-11 21:27:46 +0800753 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300754 vdbg_printk(TPACPI_DBG_INIT,
755 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800756 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300757
758 memset(&device_found, 0, sizeof(device_found));
759 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
760 (void *)name, &device_found);
761
762 *handle = NULL;
763
764 if (ACPI_SUCCESS(status)) {
765 *handle = device_found;
766 dbg_printk(TPACPI_DBG_INIT,
767 "Found ACPI handle for %s\n", name);
768 } else {
769 vdbg_printk(TPACPI_DBG_INIT,
770 "Could not locate an ACPI handle for %s: %s\n",
771 name, acpi_format_exception(status));
772 }
773}
774
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300775static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300776{
777 struct ibm_struct *ibm = data;
778
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300779 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
780 return;
781
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300782 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300783 return;
784
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300785 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300786}
787
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300788static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300789{
790 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300791 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300792
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300793 BUG_ON(!ibm->acpi);
794
795 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300796 return 0;
797
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300798 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300799 "setting up ACPI notify for %s\n", ibm->name);
800
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300801 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
802 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700803 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300804 return -ENODEV;
805 }
806
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700807 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300808 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200809 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300810 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300811
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300812 status = acpi_install_notify_handler(*ibm->acpi->handle,
813 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300814 if (ACPI_FAILURE(status)) {
815 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300816 pr_notice("another device driver is already handling %s events\n",
817 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300818 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700819 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300820 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300821 }
822 return -ENODEV;
823 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300824 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300825 return 0;
826}
827
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300828static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300829{
830 return 0;
831}
832
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300833static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300834{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300835 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300836
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300837 dbg_printk(TPACPI_DBG_INIT,
838 "registering %s as an ACPI driver\n", ibm->name);
839
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300840 BUG_ON(!ibm->acpi);
841
842 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
843 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700844 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300845 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300846 }
847
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200848 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300849 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200850
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300851 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300852
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300853 rc = acpi_bus_register_driver(ibm->acpi->driver);
854 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700855 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200856 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300857 kfree(ibm->acpi->driver);
858 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300859 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300860 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300862 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300863}
864
865
866/****************************************************************************
867 ****************************************************************************
868 *
869 * Procfs Helpers
870 *
871 ****************************************************************************
872 ****************************************************************************/
873
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200874static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300875{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200876 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300877
878 if (!ibm || !ibm->read)
879 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200880 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300881}
882
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200883static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300884{
Al Virod9dda782013-03-31 18:16:14 -0400885 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200886}
887
888static ssize_t dispatch_proc_write(struct file *file,
889 const char __user *userbuf,
890 size_t count, loff_t *pos)
891{
Al Virod9dda782013-03-31 18:16:14 -0400892 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300893 char *kernbuf;
894 int ret;
895
896 if (!ibm || !ibm->write)
897 return -EINVAL;
Andy Shevchenkodf11f6c2020-07-15 12:40:07 +0300898 if (count > PAGE_SIZE - 1)
899 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300900
Andy Shevchenkodf11f6c2020-07-15 12:40:07 +0300901 kernbuf = kmalloc(count + 1, GFP_KERNEL);
902 if (!kernbuf)
903 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300904
Andy Shevchenkodf11f6c2020-07-15 12:40:07 +0300905 if (copy_from_user(kernbuf, userbuf, count)) {
906 kfree(kernbuf);
907 return -EFAULT;
908 }
909
910 kernbuf[count] = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300911 ret = ibm->write(kernbuf);
912 if (ret == 0)
913 ret = count;
914
915 kfree(kernbuf);
916
917 return ret;
918}
919
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800920static const struct proc_ops dispatch_proc_ops = {
921 .proc_open = dispatch_proc_open,
922 .proc_read = seq_read,
923 .proc_lseek = seq_lseek,
924 .proc_release = single_release,
925 .proc_write = dispatch_proc_write,
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200926};
927
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300928/****************************************************************************
929 ****************************************************************************
930 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300931 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300932 *
933 ****************************************************************************
934 ****************************************************************************/
935
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300936static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300937static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700938static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300939static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300940static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200941static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300942
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200943#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200944static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200945{
946 struct ibm_struct *ibm, *itmp;
947
948 list_for_each_entry_safe(ibm, itmp,
949 &tpacpi_all_drivers,
950 all_drivers) {
951 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200952 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200953 }
954
955 return 0;
956}
957
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200958static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300959{
960 struct ibm_struct *ibm, *itmp;
961
962 list_for_each_entry_safe(ibm, itmp,
963 &tpacpi_all_drivers,
964 all_drivers) {
965 if (ibm->resume)
966 (ibm->resume)();
967 }
968
969 return 0;
970}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200971#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300972
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200973static SIMPLE_DEV_PM_OPS(tpacpi_pm,
974 tpacpi_suspend_handler, tpacpi_resume_handler);
975
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200976static void tpacpi_shutdown_handler(struct platform_device *pdev)
977{
978 struct ibm_struct *ibm, *itmp;
979
980 list_for_each_entry_safe(ibm, itmp,
981 &tpacpi_all_drivers,
982 all_drivers) {
983 if (ibm->shutdown)
984 (ibm->shutdown)();
985 }
986}
987
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300988static struct platform_driver tpacpi_pdriver = {
989 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200990 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200991 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300992 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200993 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300994};
995
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300996static struct platform_driver tpacpi_hwmon_pdriver = {
997 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200998 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300999 },
1000};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001001
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03001002/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001003 * sysfs support helpers
1004 */
1005
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001006static int parse_strtoul(const char *buf,
1007 unsigned long max, unsigned long *value)
1008{
1009 char *endp;
1010
André Goddard Rosae7d28602009-12-14 18:01:06 -08001011 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1012 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001013 if (*endp || *value > max)
1014 return -EINVAL;
1015
1016 return 0;
1017}
1018
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001019static void tpacpi_disable_brightness_delay(void)
1020{
1021 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001022 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001023}
1024
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001025static void printk_deprecated_attribute(const char * const what,
1026 const char * const details)
1027{
1028 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001029 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001030 what, details);
1031}
1032
Johannes Berg19d337d2009-06-02 13:01:37 +02001033/*************************************************************************
1034 * rfkill and radio control support helpers
1035 */
1036
1037/*
1038 * ThinkPad-ACPI firmware handling model:
1039 *
1040 * WLSW (master wireless switch) is event-driven, and is common to all
1041 * firmware-controlled radios. It cannot be controlled, just monitored,
1042 * as expected. It overrides all radio state in firmware
1043 *
1044 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1045 * (TODO: verify how WLSW interacts with the returned radio state).
1046 *
1047 * The only time there are shadow radio state changes, is when
1048 * masked-off hotkeys are used.
1049 */
1050
1051/*
1052 * Internal driver API for radio state:
1053 *
1054 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1055 * bool: true means radio blocked (off)
1056 */
1057enum tpacpi_rfkill_state {
1058 TPACPI_RFK_RADIO_OFF = 0,
1059 TPACPI_RFK_RADIO_ON
1060};
1061
1062/* rfkill switches */
1063enum tpacpi_rfk_id {
1064 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1065 TPACPI_RFK_WWAN_SW_ID,
1066 TPACPI_RFK_UWB_SW_ID,
1067 TPACPI_RFK_SW_MAX
1068};
1069
1070static const char *tpacpi_rfkill_names[] = {
1071 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1072 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1073 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1074 [TPACPI_RFK_SW_MAX] = NULL
1075};
1076
1077/* ThinkPad-ACPI rfkill subdriver */
1078struct tpacpi_rfk {
1079 struct rfkill *rfkill;
1080 enum tpacpi_rfk_id id;
1081 const struct tpacpi_rfk_ops *ops;
1082};
1083
1084struct tpacpi_rfk_ops {
1085 /* firmware interface */
1086 int (*get_status)(void);
1087 int (*set_status)(const enum tpacpi_rfkill_state);
1088};
1089
1090static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1091
1092/* Query FW and update rfkill sw state for a given rfkill switch */
1093static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1094{
1095 int status;
1096
1097 if (!tp_rfk)
1098 return -ENODEV;
1099
1100 status = (tp_rfk->ops->get_status)();
1101 if (status < 0)
1102 return status;
1103
1104 rfkill_set_sw_state(tp_rfk->rfkill,
1105 (status == TPACPI_RFK_RADIO_OFF));
1106
1107 return status;
1108}
1109
Johannes Berg19d337d2009-06-02 13:01:37 +02001110/*
1111 * Sync the HW-blocking state of all rfkill switches,
1112 * do notice it causes the rfkill core to schedule uevents
1113 */
1114static void tpacpi_rfk_update_hwblock_state(bool blocked)
1115{
1116 unsigned int i;
1117 struct tpacpi_rfk *tp_rfk;
1118
1119 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1120 tp_rfk = tpacpi_rfkill_switches[i];
1121 if (tp_rfk) {
1122 if (rfkill_set_hw_state(tp_rfk->rfkill,
1123 blocked)) {
1124 /* ignore -- we track sw block */
1125 }
1126 }
1127 }
1128}
1129
1130/* Call to get the WLSW state from the firmware */
1131static int hotkey_get_wlsw(void);
1132
1133/* Call to query WLSW state and update all rfkill switches */
1134static bool tpacpi_rfk_check_hwblock_state(void)
1135{
1136 int res = hotkey_get_wlsw();
1137 int hw_blocked;
1138
1139 /* When unknown or unsupported, we have to assume it is unblocked */
1140 if (res < 0)
1141 return false;
1142
1143 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1144 tpacpi_rfk_update_hwblock_state(hw_blocked);
1145
1146 return hw_blocked;
1147}
1148
1149static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1150{
1151 struct tpacpi_rfk *tp_rfk = data;
1152 int res;
1153
1154 dbg_printk(TPACPI_DBG_RFKILL,
1155 "request to change radio state to %s\n",
1156 blocked ? "blocked" : "unblocked");
1157
1158 /* try to set radio state */
1159 res = (tp_rfk->ops->set_status)(blocked ?
1160 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1161
1162 /* and update the rfkill core with whatever the FW really did */
1163 tpacpi_rfk_update_swstate(tp_rfk);
1164
1165 return (res < 0) ? res : 0;
1166}
1167
1168static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1169 .set_block = tpacpi_rfk_hook_set_block,
1170};
1171
1172static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1173 const struct tpacpi_rfk_ops *tp_rfkops,
1174 const enum rfkill_type rfktype,
1175 const char *name,
1176 const bool set_default)
1177{
1178 struct tpacpi_rfk *atp_rfk;
1179 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001180 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001181 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001182 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001183
1184 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1185
Johannes Berg19d337d2009-06-02 13:01:37 +02001186 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1187 if (atp_rfk)
1188 atp_rfk->rfkill = rfkill_alloc(name,
1189 &tpacpi_pdev->dev,
1190 rfktype,
1191 &tpacpi_rfk_rfkill_ops,
1192 atp_rfk);
1193 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001194 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001195 kfree(atp_rfk);
1196 return -ENOMEM;
1197 }
1198
1199 atp_rfk->id = id;
1200 atp_rfk->ops = tp_rfkops;
1201
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001202 sw_status = (tp_rfkops->get_status)();
1203 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001204 pr_err("failed to read initial state for %s, error %d\n",
1205 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001206 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001207 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001208 if (set_default) {
1209 /* try to keep the initial state, since we ask the
1210 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001211 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001212 }
1213 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001214 hw_state = tpacpi_rfk_check_hwblock_state();
1215 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001216
1217 res = rfkill_register(atp_rfk->rfkill);
1218 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001219 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001220 rfkill_destroy(atp_rfk->rfkill);
1221 kfree(atp_rfk);
1222 return res;
1223 }
1224
1225 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001226
Joe Perches0978e012011-04-04 10:06:25 -07001227 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001228 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001229 return 0;
1230}
1231
1232static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1233{
1234 struct tpacpi_rfk *tp_rfk;
1235
1236 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1237
1238 tp_rfk = tpacpi_rfkill_switches[id];
1239 if (tp_rfk) {
1240 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001241 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001242 tpacpi_rfkill_switches[id] = NULL;
1243 kfree(tp_rfk);
1244 }
1245}
1246
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001247static void printk_deprecated_rfkill_attribute(const char * const what)
1248{
1249 printk_deprecated_attribute(what,
1250 "Please switch to generic rfkill before year 2010");
1251}
1252
Johannes Berg19d337d2009-06-02 13:01:37 +02001253/* sysfs <radio> enable ------------------------------------------------ */
1254static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1255 struct device_attribute *attr,
1256 char *buf)
1257{
1258 int status;
1259
1260 printk_deprecated_rfkill_attribute(attr->attr.name);
1261
1262 /* This is in the ABI... */
1263 if (tpacpi_rfk_check_hwblock_state()) {
1264 status = TPACPI_RFK_RADIO_OFF;
1265 } else {
1266 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1267 if (status < 0)
1268 return status;
1269 }
1270
Ye Guojin33ce79b2021-10-18 09:17:50 +00001271 return sysfs_emit(buf, "%d\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001272 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1273}
1274
1275static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1276 struct device_attribute *attr,
1277 const char *buf, size_t count)
1278{
1279 unsigned long t;
1280 int res;
1281
1282 printk_deprecated_rfkill_attribute(attr->attr.name);
1283
1284 if (parse_strtoul(buf, 1, &t))
1285 return -EINVAL;
1286
1287 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1288
1289 /* This is in the ABI... */
1290 if (tpacpi_rfk_check_hwblock_state() && !!t)
1291 return -EPERM;
1292
1293 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1294 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1295 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1296
1297 return (res < 0) ? res : count;
1298}
1299
1300/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001301static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001302{
Johannes Berg19d337d2009-06-02 13:01:37 +02001303 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001304 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001305 else {
1306 int status;
1307
1308 /* This is in the ABI... */
1309 if (tpacpi_rfk_check_hwblock_state()) {
1310 status = TPACPI_RFK_RADIO_OFF;
1311 } else {
1312 status = tpacpi_rfk_update_swstate(
1313 tpacpi_rfkill_switches[id]);
1314 if (status < 0)
1315 return status;
1316 }
1317
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001318 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001319 (status == TPACPI_RFK_RADIO_ON) ?
1320 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001321 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001322 }
1323
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001324 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001325}
1326
1327static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1328{
1329 char *cmd;
1330 int status = -1;
1331 int res = 0;
1332
1333 if (id >= TPACPI_RFK_SW_MAX)
1334 return -ENODEV;
1335
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03001336 while ((cmd = strsep(&buf, ","))) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001337 if (strlencmp(cmd, "enable") == 0)
1338 status = TPACPI_RFK_RADIO_ON;
1339 else if (strlencmp(cmd, "disable") == 0)
1340 status = TPACPI_RFK_RADIO_OFF;
1341 else
1342 return -EINVAL;
1343 }
1344
1345 if (status != -1) {
1346 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1347 (status == TPACPI_RFK_RADIO_ON) ?
1348 "enable" : "disable",
1349 tpacpi_rfkill_names[id]);
1350 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1351 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1352 }
1353
1354 return res;
1355}
1356
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001357/*************************************************************************
1358 * thinkpad-acpi driver attributes
1359 */
1360
1361/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001362static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001363{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001364 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001365}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001366static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001367
1368/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001369static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001370{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001371 return sysfs_emit(buf, "0x%04x\n", dbg_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001372}
1373
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001374static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1375 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001376{
1377 unsigned long t;
1378
1379 if (parse_strtoul(buf, 0xffff, &t))
1380 return -EINVAL;
1381
1382 dbg_level = t;
1383
1384 return count;
1385}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001386static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001387
1388/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001389static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001390{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001391 return sysfs_emit(buf, "%s v%s\n",
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001392 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001393}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001394static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001395
1396/* --------------------------------------------------------------------- */
1397
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001398#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1399
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001400/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001401static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001402{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001403 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001404}
1405
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001406static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1407 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001408{
1409 unsigned long t;
1410
1411 if (parse_strtoul(buf, 1, &t))
1412 return -EINVAL;
1413
Johannes Berg19d337d2009-06-02 13:01:37 +02001414 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001415 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001416 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1417 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001418
1419 return count;
1420}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001421static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001422
1423/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001424static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001425{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001426 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001427}
1428
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001429static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1430 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001431{
1432 unsigned long t;
1433
1434 if (parse_strtoul(buf, 1, &t))
1435 return -EINVAL;
1436
1437 tpacpi_bluetooth_emulstate = !!t;
1438
1439 return count;
1440}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001441static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001442
1443/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001444static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001445{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001446 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001447}
1448
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001449static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1450 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001451{
1452 unsigned long t;
1453
1454 if (parse_strtoul(buf, 1, &t))
1455 return -EINVAL;
1456
1457 tpacpi_wwan_emulstate = !!t;
1458
1459 return count;
1460}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001461static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001462
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001463/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001464static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001465{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001466 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001467}
1468
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001469static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1470 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001471{
1472 unsigned long t;
1473
1474 if (parse_strtoul(buf, 1, &t))
1475 return -EINVAL;
1476
1477 tpacpi_uwb_emulstate = !!t;
1478
1479 return count;
1480}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001481static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001482#endif
1483
1484/* --------------------------------------------------------------------- */
1485
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001486static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001487 &driver_attr_debug_level, &driver_attr_version,
1488 &driver_attr_interface_version,
1489};
1490
1491static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1492{
1493 int i, res;
1494
1495 i = 0;
1496 res = 0;
1497 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1498 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1499 i++;
1500 }
1501
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001502#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1503 if (!res && dbg_wlswemul)
1504 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1505 if (!res && dbg_bluetoothemul)
1506 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1507 if (!res && dbg_wwanemul)
1508 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001509 if (!res && dbg_uwbemul)
1510 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001511#endif
1512
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001513 return res;
1514}
1515
1516static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1517{
1518 int i;
1519
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001520 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001521 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001522
1523#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1524 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1525 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1526 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001527 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001528#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001529}
1530
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001531/*************************************************************************
1532 * Firmware Data
1533 */
1534
1535/*
1536 * Table of recommended minimum BIOS versions
1537 *
1538 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001539 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001540 * bugs and bad ACPI behaviour on older versions
1541 *
1542 * 2. BIOS or EC fw with known bugs that trigger on Linux
1543 *
1544 * 3. BIOS with known reduced functionality in older versions
1545 *
1546 * We recommend the latest BIOS and EC version.
1547 * We only support the latest BIOS and EC fw version as a rule.
1548 *
1549 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1550 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001551 *
1552 * WARNING: we use this table also to detect that the machine is
1553 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001554 */
1555
1556#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1557 { .vendor = (__v), \
1558 .bios = TPID(__id1, __id2), \
1559 .ec = TPACPI_MATCH_ANY, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001560 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1561 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001562
1563#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001564 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001565 { .vendor = (__v), \
1566 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001567 .ec = __eid, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001568 .quirks = TPVER(__ev1, __ev2) << 16 \
1569 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001570
1571#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1572 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1573
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001574/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001575#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1576 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001577 __bv1, __bv2, TPID(__id1, __id2), \
1578 __ev1, __ev2), \
1579 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1580 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1581 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001582
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001583/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001584#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1585 __eid1, __eid2, __ev1, __ev2) \
1586 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001587 __bv1, __bv2, TPID(__eid1, __eid2), \
1588 __ev1, __ev2), \
1589 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1590 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1591 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001592
1593#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1594 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1595
1596#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1597 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001598 __bv1, __bv2, TPID(__id1, __id2), \
1599 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001600
1601#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1602 __eid1, __eid2, __ev1, __ev2) \
1603 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001604 __bv1, __bv2, TPID(__eid1, __eid2), \
1605 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001606
1607static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1608 /* Numeric models ------------------ */
1609 /* FW MODEL BIOS VERS */
1610 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1611 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1612 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1613 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1614 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1615 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1616 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1617 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1618 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1619
1620 /* A-series ------------------------- */
1621 /* FW MODEL BIOS VERS EC VERS */
1622 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1623 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1624 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1625 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1626 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1627 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1628 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1629 TPV_QI0('1', '3', '2', '0'), /* A22m */
1630 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1631 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1632 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1633
1634 /* G-series ------------------------- */
1635 /* FW MODEL BIOS VERS */
1636 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1637 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1638
1639 /* R-series, T-series --------------- */
1640 /* FW MODEL BIOS VERS EC VERS */
1641 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1642 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1643 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1644 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1645 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1646 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1647 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1648 T40/p, T41/p, T42/p (1) */
1649 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1650 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1651 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1652 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1653
1654 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1655 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1656 TPV_QI0('1', '6', '3', '2'), /* T22 */
1657 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1658 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1659 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1660
1661 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1662 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001663 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001664
1665 /* BIOS FW BIOS VERS EC FW EC VERS */
1666 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1667 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1668
1669 /* X-series ------------------------- */
1670 /* FW MODEL BIOS VERS EC VERS */
1671 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1672 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1673 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1674 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1675 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1676 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1677 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1678
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001679 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1680 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001681
1682 /* (0) - older versions lack DMI EC fw string and functionality */
1683 /* (1) - older versions known to lack functionality */
1684};
1685
1686#undef TPV_QL1
1687#undef TPV_QL0
1688#undef TPV_QI2
1689#undef TPV_QI1
1690#undef TPV_QI0
1691#undef TPV_Q_X
1692#undef TPV_Q
1693
1694static void __init tpacpi_check_outdated_fw(void)
1695{
1696 unsigned long fwvers;
1697 u16 ec_version, bios_version;
1698
1699 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1700 ARRAY_SIZE(tpacpi_bios_version_qtable));
1701
1702 if (!fwvers)
1703 return;
1704
1705 bios_version = fwvers & 0xffffU;
1706 ec_version = (fwvers >> 16) & 0xffffU;
1707
1708 /* note that unknown versions are set to 0x0000 and we use that */
1709 if ((bios_version > thinkpad_id.bios_release) ||
1710 (ec_version > thinkpad_id.ec_release &&
Jouke Witteveen846a4162018-07-11 11:44:41 +02001711 ec_version != TPACPI_MATCH_ANY_VERSION)) {
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001712 /*
1713 * The changelogs would let us track down the exact
1714 * reason, but it is just too much of a pain to track
1715 * it. We only list BIOSes that are either really
1716 * broken, or really stable to begin with, so it is
1717 * best if the user upgrades the firmware anyway.
1718 */
Joe Perches0978e012011-04-04 10:06:25 -07001719 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001720 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001721 }
1722}
1723
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001724static bool __init tpacpi_is_fw_known(void)
1725{
1726 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1727 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1728}
1729
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001730/****************************************************************************
1731 ****************************************************************************
1732 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001733 * Subdrivers
1734 *
1735 ****************************************************************************
1736 ****************************************************************************/
1737
1738/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001739 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001740 */
1741
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001742static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001744 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1745 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001749static struct ibm_struct thinkpad_acpi_driver_data = {
1750 .name = "driver",
1751 .read = thinkpad_acpi_driver_read,
1752};
1753
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001754/*************************************************************************
1755 * Hotkey subdriver
1756 */
1757
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001758/*
1759 * ThinkPad firmware event model
1760 *
1761 * The ThinkPad firmware has two main event interfaces: normal ACPI
1762 * notifications (which follow the ACPI standard), and a private event
1763 * interface.
1764 *
1765 * The private event interface also issues events for the hotkeys. As
1766 * the driver gained features, the event handling code ended up being
1767 * built around the hotkey subdriver. This will need to be refactored
1768 * to a more formal event API eventually.
1769 *
1770 * Some "hotkeys" are actually supposed to be used as event reports,
1771 * such as "brightness has changed", "volume has changed", depending on
1772 * the ThinkPad model and how the firmware is operating.
1773 *
1774 * Unlike other classes, hotkey-class events have mask/unmask control on
1775 * non-ancient firmware. However, how it behaves changes a lot with the
1776 * firmware model and version.
1777 */
1778
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001779enum { /* hot key scan codes (derived from ACPI DSDT) */
1780 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1781 TP_ACPI_HOTKEYSCAN_FNF2,
1782 TP_ACPI_HOTKEYSCAN_FNF3,
1783 TP_ACPI_HOTKEYSCAN_FNF4,
1784 TP_ACPI_HOTKEYSCAN_FNF5,
1785 TP_ACPI_HOTKEYSCAN_FNF6,
1786 TP_ACPI_HOTKEYSCAN_FNF7,
1787 TP_ACPI_HOTKEYSCAN_FNF8,
1788 TP_ACPI_HOTKEYSCAN_FNF9,
1789 TP_ACPI_HOTKEYSCAN_FNF10,
1790 TP_ACPI_HOTKEYSCAN_FNF11,
1791 TP_ACPI_HOTKEYSCAN_FNF12,
1792 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1793 TP_ACPI_HOTKEYSCAN_FNINSERT,
1794 TP_ACPI_HOTKEYSCAN_FNDELETE,
1795 TP_ACPI_HOTKEYSCAN_FNHOME,
1796 TP_ACPI_HOTKEYSCAN_FNEND,
1797 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1798 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1799 TP_ACPI_HOTKEYSCAN_FNSPACE,
1800 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1801 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1802 TP_ACPI_HOTKEYSCAN_MUTE,
1803 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001804 TP_ACPI_HOTKEYSCAN_UNK1,
1805 TP_ACPI_HOTKEYSCAN_UNK2,
1806 TP_ACPI_HOTKEYSCAN_UNK3,
1807 TP_ACPI_HOTKEYSCAN_UNK4,
1808 TP_ACPI_HOTKEYSCAN_UNK5,
1809 TP_ACPI_HOTKEYSCAN_UNK6,
1810 TP_ACPI_HOTKEYSCAN_UNK7,
1811 TP_ACPI_HOTKEYSCAN_UNK8,
1812
Christian Kellner149c8c72017-02-28 17:10:56 +01001813 /* Adaptive keyboard keycodes */
1814 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1815 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001816 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1817 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1818 TP_ACPI_HOTKEYSCAN_CLOUD,
1819 TP_ACPI_HOTKEYSCAN_UNK9,
1820 TP_ACPI_HOTKEYSCAN_VOICE,
1821 TP_ACPI_HOTKEYSCAN_UNK10,
1822 TP_ACPI_HOTKEYSCAN_GESTURES,
1823 TP_ACPI_HOTKEYSCAN_UNK11,
1824 TP_ACPI_HOTKEYSCAN_UNK12,
1825 TP_ACPI_HOTKEYSCAN_UNK13,
1826 TP_ACPI_HOTKEYSCAN_CONFIG,
1827 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1828 TP_ACPI_HOTKEYSCAN_RELOAD,
1829 TP_ACPI_HOTKEYSCAN_BACK,
1830 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1831 TP_ACPI_HOTKEYSCAN_MIC_UP,
1832 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1833 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1834 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1835
Christian Kellner696c65232017-02-28 17:10:57 +01001836 /* Lenovo extended keymap, starting at 0x1300 */
1837 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1838 /* first new observed key (star, favorites) is 0x1311 */
1839 TP_ACPI_HOTKEYSCAN_STAR = 69,
1840 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
Benjamin Bergcb5c1972018-06-20 15:49:27 +02001841 TP_ACPI_HOTKEYSCAN_CALCULATOR,
Christian Kellner696c65232017-02-28 17:10:57 +01001842 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1843 TP_ACPI_HOTKEYSCAN_KEYBOARD,
Hans de Goede7ed77482020-09-08 12:21:11 -07001844 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1845 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1846 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1847 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
Christian Kellner696c65232017-02-28 17:10:57 +01001848
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001849 /* Hotkey keymap size */
1850 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001851};
1852
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001853enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001854 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1855 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1856};
1857
1858enum { /* Positions of some of the keys in hotkey masks */
1859 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1860 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1861 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1862 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1863 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001864 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001865 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1866 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1867 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1868 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1869 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1870};
1871
1872enum { /* NVRAM to ACPI HKEY group map */
1873 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1874 TP_ACPI_HKEY_ZOOM_MASK |
1875 TP_ACPI_HKEY_DISPSWTCH_MASK |
1876 TP_ACPI_HKEY_HIBERNATE_MASK,
1877 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1878 TP_ACPI_HKEY_BRGHTDWN_MASK,
1879 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1880 TP_ACPI_HKEY_VOLDWN_MASK |
1881 TP_ACPI_HKEY_MUTE_MASK,
1882};
1883
1884#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1885struct tp_nvram_state {
1886 u16 thinkpad_toggle:1;
1887 u16 zoom_toggle:1;
1888 u16 display_toggle:1;
1889 u16 thinklight_toggle:1;
1890 u16 hibernate_toggle:1;
1891 u16 displayexp_toggle:1;
1892 u16 display_state:1;
1893 u16 brightness_toggle:1;
1894 u16 volume_toggle:1;
1895 u16 mute:1;
1896
1897 u8 brightness_level;
1898 u8 volume_level;
1899};
1900
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001901/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001902static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001903
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001904/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001905 * Acquire mutex to write poller control variables as an
1906 * atomic block.
1907 *
1908 * Increment hotkey_config_change when changing them if you
1909 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001910 *
1911 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1912 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001913static struct mutex hotkey_thread_data_mutex;
1914static unsigned int hotkey_config_change;
1915
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001916/*
1917 * hotkey poller control variables
1918 *
1919 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001920 *
1921 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1922 * should be used only when the changes need to be taken as
1923 * a block, OR when one needs to force the kthread to forget
1924 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001925 */
1926static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1927static unsigned int hotkey_poll_freq = 10; /* Hz */
1928
1929#define HOTKEY_CONFIG_CRITICAL_START \
1930 do { \
1931 mutex_lock(&hotkey_thread_data_mutex); \
1932 hotkey_config_change++; \
1933 } while (0);
1934#define HOTKEY_CONFIG_CRITICAL_END \
1935 mutex_unlock(&hotkey_thread_data_mutex);
1936
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001937#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1938
1939#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001940#define HOTKEY_CONFIG_CRITICAL_START
1941#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001942
1943#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1944
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001945static struct mutex hotkey_mutex;
1946
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001947static enum { /* Reasons for waking up */
1948 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1949 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1950 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1951} hotkey_wakeup_reason;
1952
1953static int hotkey_autosleep_ack;
1954
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001955static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1956static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04001957static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001958static u32 hotkey_reserved_mask; /* events better left disabled */
1959static u32 hotkey_driver_mask; /* events needed by the driver */
1960static u32 hotkey_user_mask; /* events visible to userspace */
1961static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001962
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001963static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001964
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03001965static void tpacpi_driver_event(const unsigned int hkey_event);
1966static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03001967static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03001968
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001969/* HKEY.MHKG() return bits */
1970#define TP_HOTKEY_TABLET_MASK (1 << 3)
Benjamin Bergdda3ec02017-09-15 15:20:49 +02001971enum {
1972 TP_ACPI_MULTI_MODE_INVALID = 0,
1973 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1974 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1975 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1976 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1977 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1978 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1979 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1980};
1981
1982enum {
1983 /* The following modes are considered tablet mode for the purpose of
1984 * reporting the status to userspace. i.e. in all these modes it makes
1985 * sense to disable the laptop input devices such as touchpad and
1986 * keyboard.
1987 */
1988 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1989 TP_ACPI_MULTI_MODE_STAND |
1990 TP_ACPI_MULTI_MODE_TENT |
1991 TP_ACPI_MULTI_MODE_STAND_TENT,
1992};
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001993
Johannes Berg19d337d2009-06-02 13:01:37 +02001994static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001995{
Johannes Berg19d337d2009-06-02 13:01:37 +02001996 int status;
1997
1998 if (!tp_features.hotkey_wlsw)
1999 return -ENODEV;
2000
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002001#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002002 if (dbg_wlswemul)
2003 return (tpacpi_wlsw_emulstate) ?
2004 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002005#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002006
2007 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002008 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002009
2010 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002011}
2012
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002013static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2014{
2015 int type = (s >> 16) & 0xffff;
2016 int value = s & 0xffff;
2017 int mode = TP_ACPI_MULTI_MODE_INVALID;
2018 int valid_modes = 0;
2019
2020 if (has_tablet_mode)
2021 *has_tablet_mode = 0;
2022
2023 switch (type) {
2024 case 1:
2025 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2026 TP_ACPI_MULTI_MODE_TABLET |
2027 TP_ACPI_MULTI_MODE_STAND_TENT;
2028 break;
2029 case 2:
2030 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2031 TP_ACPI_MULTI_MODE_FLAT |
2032 TP_ACPI_MULTI_MODE_TABLET |
2033 TP_ACPI_MULTI_MODE_STAND |
2034 TP_ACPI_MULTI_MODE_TENT;
2035 break;
2036 case 3:
2037 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2038 TP_ACPI_MULTI_MODE_FLAT;
2039 break;
2040 case 4:
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002041 case 5:
Benjamin Berg26befef2017-11-14 17:14:14 +01002042 /* In mode 4, FLAT is not specified as a valid mode. However,
2043 * it can be seen at least on the X1 Yoga 2nd Generation.
2044 */
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002045 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2046 TP_ACPI_MULTI_MODE_FLAT |
2047 TP_ACPI_MULTI_MODE_TABLET |
2048 TP_ACPI_MULTI_MODE_STAND |
2049 TP_ACPI_MULTI_MODE_TENT;
2050 break;
2051 default:
2052 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2053 type, value, TPACPI_MAIL);
2054 return 0;
2055 }
2056
2057 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2058 *has_tablet_mode = 1;
2059
2060 switch (value) {
2061 case 1:
2062 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2063 break;
2064 case 2:
2065 mode = TP_ACPI_MULTI_MODE_FLAT;
2066 break;
2067 case 3:
2068 mode = TP_ACPI_MULTI_MODE_TABLET;
2069 break;
2070 case 4:
2071 if (type == 1)
2072 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2073 else
2074 mode = TP_ACPI_MULTI_MODE_STAND;
2075 break;
2076 case 5:
2077 mode = TP_ACPI_MULTI_MODE_TENT;
2078 break;
2079 default:
2080 if (type == 5 && value == 0xffff) {
2081 pr_warn("Multi mode status is undetected, assuming laptop\n");
2082 return 0;
2083 }
2084 }
2085
2086 if (!(mode & valid_modes)) {
2087 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2088 value, type, TPACPI_MAIL);
2089 return 0;
2090 }
2091
2092 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2093}
2094
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002095static int hotkey_get_tablet_mode(int *status)
2096{
2097 int s;
2098
Lyudeb03f4d42016-11-11 15:15:03 -05002099 switch (tp_features.hotkey_tablet) {
2100 case TP_HOTKEY_TABLET_USES_MHKG:
2101 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2102 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002103
Lyudeb03f4d42016-11-11 15:15:03 -05002104 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2105 break;
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002106 case TP_HOTKEY_TABLET_USES_GMMS:
2107 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
Lyudeb03f4d42016-11-11 15:15:03 -05002108 return -EIO;
2109
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002110 *status = hotkey_gmms_get_tablet_mode(s, NULL);
Lyudeb03f4d42016-11-11 15:15:03 -05002111 break;
2112 default:
2113 break;
2114 }
2115
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002116 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002117}
2118
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002119/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002120 * Reads current event mask from firmware, and updates
2121 * hotkey_acpi_mask accordingly. Also resets any bits
2122 * from hotkey_user_mask that are unavailable to be
2123 * delivered (shadow requirement of the userspace ABI).
2124 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002125 * Call with hotkey_mutex held
2126 */
2127static int hotkey_mask_get(void)
2128{
2129 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130 u32 m = 0;
2131
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002132 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002133 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002134
2135 hotkey_acpi_mask = m;
2136 } else {
2137 /* no mask support doesn't mean no event support... */
2138 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002140
2141 /* sync userspace-visible mask */
2142 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002143
2144 return 0;
2145}
2146
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002147static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148{
2149 /* log only what the user can fix... */
2150 const u32 wantedmask = hotkey_driver_mask &
2151 ~(hotkey_acpi_mask | hotkey_source_mask) &
2152 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2153
2154 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002155 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002156}
2157
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002158/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002159 * Set the firmware mask when supported
2160 *
2161 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2162 *
2163 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2164 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002165 * Call with hotkey_mutex held
2166 */
2167static int hotkey_mask_set(u32 mask)
2168{
2169 int i;
2170 int rc = 0;
2171
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002172 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002173
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002174 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002175 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002176 if (!acpi_evalf(hkey_handle,
2177 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002178 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002179 rc = -EIO;
2180 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 }
2182 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002183 }
2184
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002185 /*
2186 * We *must* make an inconditional call to hotkey_mask_get to
2187 * refresh hotkey_acpi_mask and update hotkey_user_mask
2188 *
2189 * Take the opportunity to also log when we cannot _enable_
2190 * a given event.
2191 */
2192 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002193 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002194 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002195 }
2196
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002197 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2198 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002199
2200 return rc;
2201}
2202
2203/*
2204 * Sets hotkey_user_mask and tries to set the firmware mask
2205 *
2206 * Call with hotkey_mutex held
2207 */
2208static int hotkey_user_mask_set(const u32 mask)
2209{
2210 int rc;
2211
2212 /* Give people a chance to notice they are doing something that
2213 * is bound to go boom on their users sooner or later */
2214 if (!tp_warned.hotkey_mask_ff &&
2215 (mask == 0xffff || mask == 0xffffff ||
2216 mask == 0xffffffff)) {
2217 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002218 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2219 mask);
2220 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002221 }
2222
2223 /* Try to enable what the user asked for, plus whatever we need.
2224 * this syncs everything but won't enable bits in hotkey_user_mask */
2225 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2226
2227 /* Enable the available bits in hotkey_user_mask */
2228 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2229
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002230 return rc;
2231}
2232
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002233/*
2234 * Sets the driver hotkey mask.
2235 *
2236 * Can be called even if the hotkey subdriver is inactive
2237 */
2238static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2239{
2240 int rc;
2241
2242 /* Do the right thing if hotkey_init has not been called yet */
2243 if (!tp_features.hotkey) {
2244 hotkey_driver_mask = mask;
2245 return 0;
2246 }
2247
2248 mutex_lock(&hotkey_mutex);
2249
2250 HOTKEY_CONFIG_CRITICAL_START
2251 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002252#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002253 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002254#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002255 HOTKEY_CONFIG_CRITICAL_END
2256
2257 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2258 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002259 hotkey_poll_setup(true);
2260
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002261 mutex_unlock(&hotkey_mutex);
2262
2263 return rc;
2264}
2265
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002266static int hotkey_status_get(int *status)
2267{
2268 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2269 return -EIO;
2270
2271 return 0;
2272}
2273
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002274static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002275{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002276 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002277 return -EIO;
2278
2279 return 0;
2280}
2281
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002282static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002283{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002284 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002285
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002286 if (tp_features.hotkey_tablet &&
2287 !hotkey_get_tablet_mode(&state)) {
2288 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002289
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002290 input_report_switch(tpacpi_inputdev,
2291 SW_TABLET_MODE, !!state);
2292 input_sync(tpacpi_inputdev);
2293
2294 mutex_unlock(&tpacpi_inputdev_send_mutex);
2295 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002296}
2297
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002298/* Do NOT call without validating scancode first */
2299static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002300{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002301 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002302
2303 if (keycode != KEY_RESERVED) {
2304 mutex_lock(&tpacpi_inputdev_send_mutex);
2305
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002306 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002307 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002308 input_sync(tpacpi_inputdev);
2309
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002310 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002311 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002312 input_sync(tpacpi_inputdev);
2313
2314 mutex_unlock(&tpacpi_inputdev_send_mutex);
2315 }
2316}
2317
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002318/* Do NOT call without validating scancode first */
2319static void tpacpi_input_send_key_masked(const unsigned int scancode)
2320{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002321 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002322 if (hotkey_user_mask & (1 << scancode))
2323 tpacpi_input_send_key(scancode);
2324}
2325
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002326#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2327static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2328
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002329/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002330static void tpacpi_hotkey_send_key(unsigned int scancode)
2331{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002332 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002333}
2334
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002335static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002336{
2337 u8 d;
2338
2339 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2340 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2341 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2342 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2343 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2344 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2345 }
Hans de Goedec685e202017-02-09 16:44:13 +01002346 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2348 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2349 }
2350 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2351 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2352 n->displayexp_toggle =
2353 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2354 }
2355 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2356 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2357 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2358 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2359 n->brightness_toggle =
2360 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2361 }
2362 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2363 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2364 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2365 >> TP_NVRAM_POS_LEVEL_VOLUME;
2366 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2367 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2368 }
2369}
2370
Behan Webstera4d44ba2014-02-12 21:58:46 +01002371#define TPACPI_COMPARE_KEY(__scancode, __member) \
2372do { \
2373 if ((event_mask & (1 << __scancode)) && \
2374 oldn->__member != newn->__member) \
2375 tpacpi_hotkey_send_key(__scancode); \
2376} while (0)
2377
2378#define TPACPI_MAY_SEND_KEY(__scancode) \
2379do { \
2380 if (event_mask & (1 << __scancode)) \
2381 tpacpi_hotkey_send_key(__scancode); \
2382} while (0)
2383
2384static void issue_volchange(const unsigned int oldvol,
2385 const unsigned int newvol,
2386 const u32 event_mask)
2387{
2388 unsigned int i = oldvol;
2389
2390 while (i > newvol) {
2391 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2392 i--;
2393 }
2394 while (i < newvol) {
2395 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2396 i++;
2397 }
2398}
2399
2400static void issue_brightnesschange(const unsigned int oldbrt,
2401 const unsigned int newbrt,
2402 const u32 event_mask)
2403{
2404 unsigned int i = oldbrt;
2405
2406 while (i > newbrt) {
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2408 i--;
2409 }
2410 while (i < newbrt) {
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2412 i++;
2413 }
2414}
2415
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002416static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2417 struct tp_nvram_state *newn,
2418 const u32 event_mask)
2419{
2420
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002421 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2422 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2423 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2424 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2425
2426 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2427
2428 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2429
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002430 /*
2431 * Handle volume
2432 *
2433 * This code is supposed to duplicate the IBM firmware behaviour:
2434 * - Pressing MUTE issues mute hotkey message, even when already mute
2435 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002436 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002437 * - The act of unmuting issues volume up/down notification,
2438 * depending which key was used to unmute
2439 *
2440 * We are constrained to what the NVRAM can tell us, which is not much
2441 * and certainly not enough if more than one volume hotkey was pressed
2442 * since the last poll cycle.
2443 *
2444 * Just to make our life interesting, some newer Lenovo ThinkPads have
2445 * bugs in the BIOS and may fail to update volume_toggle properly.
2446 */
2447 if (newn->mute) {
2448 /* muted */
2449 if (!oldn->mute ||
2450 oldn->volume_toggle != newn->volume_toggle ||
2451 oldn->volume_level != newn->volume_level) {
2452 /* recently muted, or repeated mute keypress, or
2453 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002454 issue_volchange(oldn->volume_level, newn->volume_level,
2455 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002456 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2457 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002458 } else {
2459 /* unmute */
2460 if (oldn->mute) {
2461 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002463 }
2464 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002465 issue_volchange(oldn->volume_level, newn->volume_level,
2466 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002467 } else if (oldn->volume_toggle != newn->volume_toggle) {
2468 /* repeated vol up/down keypress at end of scale ? */
2469 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002471 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002473 }
2474 }
2475
2476 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002477 if (oldn->brightness_level != newn->brightness_level) {
2478 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002479 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002480 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2481 /* repeated key presses that didn't change state */
2482 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002484 else if (newn->brightness_level >= bright_maxlvl
2485 && !tp_features.bright_unkfw)
2486 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002487 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488
2489#undef TPACPI_COMPARE_KEY
2490#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002491}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002492
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002493/*
2494 * Polling driver
2495 *
2496 * We track all events in hotkey_source_mask all the time, since
2497 * most of them are edge-based. We only issue those requested by
2498 * hotkey_user_mask or hotkey_driver_mask, though.
2499 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500static int hotkey_kthread(void *data)
2501{
Tom Rix5f38b062020-09-13 12:02:03 -07002502 struct tp_nvram_state s[2] = { 0 };
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002503 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002504 unsigned int si, so;
2505 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002506 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002507 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002508 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002509
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2511 goto exit;
2512
2513 set_freezable();
2514
2515 so = 0;
2516 si = 1;
2517 t = 0;
2518
2519 /* Initial state for compares */
2520 mutex_lock(&hotkey_thread_data_mutex);
2521 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002522 poll_mask = hotkey_source_mask;
2523 event_mask = hotkey_source_mask &
2524 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002525 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002526 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002527 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002528
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002529 while (!kthread_should_stop()) {
2530 if (t == 0) {
2531 if (likely(poll_freq))
2532 t = 1000/poll_freq;
2533 else
2534 t = 100; /* should never happen... */
2535 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002536 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002537 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002538 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002539
2540 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002541 continue;
2542
2543 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002544 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002545 /* forget old state on thaw or config change */
2546 si = so;
2547 t = 0;
2548 change_detector = hotkey_config_change;
2549 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002550 poll_mask = hotkey_source_mask;
2551 event_mask = hotkey_source_mask &
2552 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002553 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002554 mutex_unlock(&hotkey_thread_data_mutex);
2555
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002556 if (likely(poll_mask)) {
2557 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002558 if (likely(si != so)) {
2559 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002560 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002561 }
2562 }
2563
2564 so = si;
2565 si ^= 1;
2566 }
2567
2568exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002569 return 0;
2570}
2571
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002572/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573static void hotkey_poll_stop_sync(void)
2574{
2575 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002576 kthread_stop(tpacpi_hotkey_task);
2577 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002578 }
2579}
2580
2581/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002582static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002584 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2585 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002586
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002587 if (hotkey_poll_freq > 0 &&
2588 (poll_driver_mask ||
2589 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002590 if (!tpacpi_hotkey_task) {
2591 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002592 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002593 if (IS_ERR(tpacpi_hotkey_task)) {
2594 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002595 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002596 }
2597 }
2598 } else {
2599 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002600 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002601 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002602 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
Joe Perches0978e012011-04-04 10:06:25 -07002603 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002604 }
2605 }
2606}
2607
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002608static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002609{
2610 mutex_lock(&hotkey_mutex);
2611 hotkey_poll_setup(may_warn);
2612 mutex_unlock(&hotkey_mutex);
2613}
2614
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002615/* call with hotkey_mutex held */
2616static void hotkey_poll_set_freq(unsigned int freq)
2617{
2618 if (!freq)
2619 hotkey_poll_stop_sync();
2620
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002621 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002622}
2623
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002624#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2625
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002626static void hotkey_poll_setup(const bool __unused)
2627{
2628}
2629
2630static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002631{
2632}
2633
2634#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2635
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002636static int hotkey_inputdev_open(struct input_dev *dev)
2637{
2638 switch (tpacpi_lifecycle) {
2639 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002640 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002641 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002642 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002643 case TPACPI_LIFE_EXITING:
2644 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002645 }
2646
2647 /* Should only happen if tpacpi_lifecycle is corrupt */
2648 BUG();
2649 return -EBUSY;
2650}
2651
2652static void hotkey_inputdev_close(struct input_dev *dev)
2653{
2654 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002655 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002656 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002657 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002658}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002659
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002660/* sysfs hotkey enable ------------------------------------------------- */
2661static ssize_t hotkey_enable_show(struct device *dev,
2662 struct device_attribute *attr,
2663 char *buf)
2664{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002665 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002666
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002667 printk_deprecated_attribute("hotkey_enable",
2668 "Hotkey reporting is always enabled");
2669
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002670 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671 if (res)
2672 return res;
2673
Ye Guojin33ce79b2021-10-18 09:17:50 +00002674 return sysfs_emit(buf, "%d\n", status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675}
2676
2677static ssize_t hotkey_enable_store(struct device *dev,
2678 struct device_attribute *attr,
2679 const char *buf, size_t count)
2680{
2681 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002682
2683 printk_deprecated_attribute("hotkey_enable",
2684 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002685
2686 if (parse_strtoul(buf, 1, &t))
2687 return -EINVAL;
2688
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002689 if (t == 0)
2690 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002692 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693}
2694
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002695static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002696
2697/* sysfs hotkey mask --------------------------------------------------- */
2698static ssize_t hotkey_mask_show(struct device *dev,
2699 struct device_attribute *attr,
2700 char *buf)
2701{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002702 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002703}
2704
2705static ssize_t hotkey_mask_store(struct device *dev,
2706 struct device_attribute *attr,
2707 const char *buf, size_t count)
2708{
2709 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002710 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002711
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002712 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713 return -EINVAL;
2714
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002715 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002716 return -ERESTARTSYS;
2717
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002718 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002719
2720#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002721 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002722#endif
2723
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002724 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002725
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002726 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2727
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002728 return (res) ? res : count;
2729}
2730
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002731static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002732
2733/* sysfs hotkey bios_enabled ------------------------------------------- */
2734static ssize_t hotkey_bios_enabled_show(struct device *dev,
2735 struct device_attribute *attr,
2736 char *buf)
2737{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002738 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002739}
2740
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002741static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002742
2743/* sysfs hotkey bios_mask ---------------------------------------------- */
2744static ssize_t hotkey_bios_mask_show(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2747{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002748 printk_deprecated_attribute("hotkey_bios_mask",
2749 "This attribute is useless.");
Ye Guojin33ce79b2021-10-18 09:17:50 +00002750 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002751}
2752
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002753static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002754
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002755/* sysfs hotkey all_mask ----------------------------------------------- */
2756static ssize_t hotkey_all_mask_show(struct device *dev,
2757 struct device_attribute *attr,
2758 char *buf)
2759{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002760 return sysfs_emit(buf, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002761 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002762}
2763
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002764static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002765
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002766/* sysfs hotkey all_mask ----------------------------------------------- */
2767static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2770{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002771 return sysfs_emit(buf, "0x%08x\n",
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002772 hotkey_adaptive_all_mask | hotkey_source_mask);
2773}
2774
2775static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2776
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002777/* sysfs hotkey recommended_mask --------------------------------------- */
2778static ssize_t hotkey_recommended_mask_show(struct device *dev,
2779 struct device_attribute *attr,
2780 char *buf)
2781{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002782 return sysfs_emit(buf, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002783 (hotkey_all_mask | hotkey_source_mask)
2784 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002785}
2786
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002787static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002788
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002789#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2790
2791/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2792static ssize_t hotkey_source_mask_show(struct device *dev,
2793 struct device_attribute *attr,
2794 char *buf)
2795{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002796 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002797}
2798
2799static ssize_t hotkey_source_mask_store(struct device *dev,
2800 struct device_attribute *attr,
2801 const char *buf, size_t count)
2802{
2803 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002804 u32 r_ev;
2805 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002806
2807 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2808 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2809 return -EINVAL;
2810
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002811 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002812 return -ERESTARTSYS;
2813
2814 HOTKEY_CONFIG_CRITICAL_START
2815 hotkey_source_mask = t;
2816 HOTKEY_CONFIG_CRITICAL_END
2817
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002818 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2819 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002820 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002821
2822 /* check if events needed by the driver got disabled */
2823 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2824 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002825
2826 mutex_unlock(&hotkey_mutex);
2827
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002828 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002829 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002830
2831 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002832 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002833 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002834
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002835 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2836
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002837 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002838}
2839
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002840static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841
2842/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2843static ssize_t hotkey_poll_freq_show(struct device *dev,
2844 struct device_attribute *attr,
2845 char *buf)
2846{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002847 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002848}
2849
2850static ssize_t hotkey_poll_freq_store(struct device *dev,
2851 struct device_attribute *attr,
2852 const char *buf, size_t count)
2853{
2854 unsigned long t;
2855
2856 if (parse_strtoul(buf, 25, &t))
2857 return -EINVAL;
2858
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002859 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002860 return -ERESTARTSYS;
2861
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002862 hotkey_poll_set_freq(t);
2863 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002864
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002865 mutex_unlock(&hotkey_mutex);
2866
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002867 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2868
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002869 return count;
2870}
2871
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002872static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002873
2874#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2875
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002876/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002877static ssize_t hotkey_radio_sw_show(struct device *dev,
2878 struct device_attribute *attr,
2879 char *buf)
2880{
Johannes Berg19d337d2009-06-02 13:01:37 +02002881 int res;
2882 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002883 if (res < 0)
2884 return res;
2885
Johannes Berg19d337d2009-06-02 13:01:37 +02002886 /* Opportunistic update */
2887 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2888
Ye Guojin33ce79b2021-10-18 09:17:50 +00002889 return sysfs_emit(buf, "%d\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02002890 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002891}
2892
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002893static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002894
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002895static void hotkey_radio_sw_notify_change(void)
2896{
2897 if (tp_features.hotkey_wlsw)
2898 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2899 "hotkey_radio_sw");
2900}
2901
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002902/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2903static ssize_t hotkey_tablet_mode_show(struct device *dev,
2904 struct device_attribute *attr,
2905 char *buf)
2906{
2907 int res, s;
2908 res = hotkey_get_tablet_mode(&s);
2909 if (res < 0)
2910 return res;
2911
Ye Guojin33ce79b2021-10-18 09:17:50 +00002912 return sysfs_emit(buf, "%d\n", !!s);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002913}
2914
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002915static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002916
2917static void hotkey_tablet_mode_notify_change(void)
2918{
2919 if (tp_features.hotkey_tablet)
2920 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2921 "hotkey_tablet_mode");
2922}
2923
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002924/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002925static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2926 struct device_attribute *attr,
2927 char *buf)
2928{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002929 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002930}
2931
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002932static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002933
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002934static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002935{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002936 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2937 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002938}
2939
2940/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002941static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2942 struct device_attribute *attr,
2943 char *buf)
2944{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002945 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002946}
2947
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002948static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2949 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002950
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002951static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002952{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002953 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2954 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002955}
2956
Bastien Nocerab790cee2015-03-02 14:45:27 +01002957/* sysfs adaptive kbd mode --------------------------------------------- */
2958
2959static int adaptive_keyboard_get_mode(void);
2960static int adaptive_keyboard_set_mode(int new_mode);
2961
2962enum ADAPTIVE_KEY_MODE {
2963 HOME_MODE,
2964 WEB_BROWSER_MODE,
2965 WEB_CONFERENCE_MODE,
2966 FUNCTION_MODE,
2967 LAYFLAT_MODE
2968};
2969
2970static ssize_t adaptive_kbd_mode_show(struct device *dev,
2971 struct device_attribute *attr,
2972 char *buf)
2973{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002974 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002975
2976 current_mode = adaptive_keyboard_get_mode();
2977 if (current_mode < 0)
2978 return current_mode;
2979
Ye Guojin33ce79b2021-10-18 09:17:50 +00002980 return sysfs_emit(buf, "%d\n", current_mode);
Bastien Nocerab790cee2015-03-02 14:45:27 +01002981}
2982
2983static ssize_t adaptive_kbd_mode_store(struct device *dev,
2984 struct device_attribute *attr,
2985 const char *buf, size_t count)
2986{
2987 unsigned long t;
2988 int res;
2989
2990 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2991 return -EINVAL;
2992
2993 res = adaptive_keyboard_set_mode(t);
2994 return (res < 0) ? res : count;
2995}
2996
2997static DEVICE_ATTR_RW(adaptive_kbd_mode);
2998
2999static struct attribute *adaptive_kbd_attributes[] = {
3000 &dev_attr_adaptive_kbd_mode.attr,
3001 NULL
3002};
3003
3004static const struct attribute_group adaptive_kbd_attr_group = {
3005 .attrs = adaptive_kbd_attributes,
3006};
3007
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003008/* --------------------------------------------------------------------- */
3009
Len Bakerc99ca782021-09-26 13:19:08 +02003010static struct attribute *hotkey_attributes[] = {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003011 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003012 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003013 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003014 &dev_attr_wakeup_reason.attr,
3015 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003016 &dev_attr_hotkey_mask.attr,
3017 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003018 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003019 &dev_attr_hotkey_recommended_mask.attr,
Hans de Goedeb68f8a12021-11-23 22:04:19 +01003020 &dev_attr_hotkey_tablet_mode.attr,
3021 &dev_attr_hotkey_radio_sw.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003022#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003023 &dev_attr_hotkey_source_mask.attr,
3024 &dev_attr_hotkey_poll_freq.attr,
3025#endif
Len Bakerc99ca782021-09-26 13:19:08 +02003026 NULL
3027};
3028
3029static umode_t hotkey_attr_is_visible(struct kobject *kobj,
3030 struct attribute *attr, int n)
3031{
3032 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
3033 if (!tp_features.hotkey_tablet)
3034 return 0;
3035 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
3036 if (!tp_features.hotkey_wlsw)
3037 return 0;
3038 }
3039
3040 return attr->mode;
3041}
3042
3043static const struct attribute_group hotkey_attr_group = {
3044 .is_visible = hotkey_attr_is_visible,
3045 .attrs = hotkey_attributes,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003046};
3047
Johannes Berg19d337d2009-06-02 13:01:37 +02003048/*
3049 * Sync both the hw and sw blocking state of all switches
3050 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003051static void tpacpi_send_radiosw_update(void)
3052{
3053 int wlsw;
3054
Johannes Berg19d337d2009-06-02 13:01:37 +02003055 /*
3056 * We must sync all rfkill controllers *before* issuing any
3057 * rfkill input events, or we will race the rfkill core input
3058 * handler.
3059 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003060 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003061 * for the above.
3062 *
3063 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3064 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003065
Johannes Berg19d337d2009-06-02 13:01:37 +02003066 wlsw = hotkey_get_wlsw();
3067
3068 /* Sync hw blocking state first if it is hw-blocked */
3069 if (wlsw == TPACPI_RFK_RADIO_OFF)
3070 tpacpi_rfk_update_hwblock_state(true);
3071
Johannes Berg19d337d2009-06-02 13:01:37 +02003072 /* Sync hw blocking state last if it is hw-unblocked */
3073 if (wlsw == TPACPI_RFK_RADIO_ON)
3074 tpacpi_rfk_update_hwblock_state(false);
3075
3076 /* Issue rfkill input event for WLSW switch */
3077 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003078 mutex_lock(&tpacpi_inputdev_send_mutex);
3079
3080 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003081 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003082 input_sync(tpacpi_inputdev);
3083
3084 mutex_unlock(&tpacpi_inputdev_send_mutex);
3085 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003086
3087 /*
3088 * this can be unconditional, as we will poll state again
3089 * if userspace uses the notify to read data
3090 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003091 hotkey_radio_sw_notify_change();
3092}
3093
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003094static void hotkey_exit(void)
3095{
3096#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003097 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003098 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003099 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003100#endif
Len Bakerc99ca782021-09-26 13:19:08 +02003101 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &hotkey_attr_group);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003102
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003103 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003104 "restoring original HKEY status and mask\n");
3105 /* yes, there is a bitwise or below, we want the
3106 * functions to be called even if one of them fail */
3107 if (((tp_features.hotkey_mask &&
3108 hotkey_mask_set(hotkey_orig_mask)) |
3109 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003110 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003111}
3112
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003113static void __init hotkey_unmap(const unsigned int scancode)
3114{
3115 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3116 clear_bit(hotkey_keycode_map[scancode],
3117 tpacpi_inputdev->keybit);
3118 hotkey_keycode_map[scancode] = KEY_RESERVED;
3119 }
3120}
3121
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003122/*
3123 * HKEY quirks:
3124 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3125 */
3126
3127#define TPACPI_HK_Q_INIMASK 0x0001
3128
3129static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3130 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3131 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3132 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3133 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3134 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3135 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3136 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3137 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3138 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3139 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3140 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3141 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3142 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3143 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3144 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3145 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3146 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3147 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3148 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3149};
3150
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003151typedef u16 tpacpi_keymap_entry_t;
3152typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003153
Lyudeb3180022016-11-11 15:15:02 -05003154static int hotkey_init_tablet_mode(void)
3155{
Darren Hartcb2bf252016-12-14 14:45:48 -08003156 int in_tablet_mode = 0, res;
3157 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003158
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003159 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3160 int has_tablet_mode;
3161
3162 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3163 &has_tablet_mode);
Hans de Goede685489a2020-11-06 15:01:30 +01003164 /*
3165 * The Yoga 11e series has 2 accelerometers described by a
3166 * BOSC0200 ACPI node. This setup relies on a Windows service
3167 * which calls special ACPI methods on this node to report
3168 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3169 * does not support this, so skip the hotkey on these models.
3170 */
Hans de Goede153cca92021-07-29 10:21:34 +02003171 if (has_tablet_mode && !dual_accel_detect())
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003172 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3173 type = "GMMS";
3174 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003175 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003176 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3177 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3178 type = "MHKG";
3179 }
3180
3181 if (!tp_features.hotkey_tablet)
3182 return 0;
3183
3184 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3185 type, in_tablet_mode ? "tablet" : "laptop");
3186
Lyudeb3180022016-11-11 15:15:02 -05003187 return in_tablet_mode;
3188}
3189
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003190static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003191{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003192 /* Requirements for changing the default keymaps:
3193 *
3194 * 1. Many of the keys are mapped to KEY_RESERVED for very
3195 * good reasons. Do not change them unless you have deep
3196 * knowledge on the IBM and Lenovo ThinkPad firmware for
3197 * the various ThinkPad models. The driver behaves
3198 * differently for KEY_RESERVED: such keys have their
3199 * hot key mask *unset* in mask_recommended, and also
3200 * in the initial hot key mask programmed into the
3201 * firmware at driver load time, which means the firm-
3202 * ware may react very differently if you change them to
3203 * something else;
3204 *
3205 * 2. You must be subscribed to the linux-thinkpad and
3206 * ibm-acpi-devel mailing lists, and you should read the
3207 * list archives since 2007 if you want to change the
3208 * keymaps. This requirement exists so that you will
3209 * know the past history of problems with the thinkpad-
3210 * acpi driver keymaps, and also that you will be
3211 * listening to any bug reports;
3212 *
3213 * 3. Do not send thinkpad-acpi specific patches directly to
3214 * for merging, *ever*. Send them to the linux-acpi
3215 * mailinglist for comments. Merging is to be done only
3216 * through acpi-test and the ACPI maintainer.
3217 *
3218 * If the above is too much to ask, don't change the keymap.
3219 * Ask the thinkpad-acpi maintainer to do it, instead.
3220 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003221
3222 enum keymap_index {
3223 TPACPI_KEYMAP_IBM_GENERIC = 0,
3224 TPACPI_KEYMAP_LENOVO_GENERIC,
3225 };
3226
3227 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3228 /* Generic keymap for IBM ThinkPads */
3229 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003230 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003231 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003232 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3233 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003234
3235 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003236 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3237 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3238 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003239
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003240 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003241 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003242 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003243
3244 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003245 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003246
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003247 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3248 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003249
3250 /* Volume: firmware always react to it and reprograms
3251 * the built-in *extra* mixer. Never map it to control
3252 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003253 KEY_RESERVED, /* 0x14: VOLUME UP */
3254 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3255 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003256
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003257 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003258
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003259 /* (assignments unknown, please report if found) */
3260 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3261 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003262
3263 /* No assignments, only used for Adaptive keyboards. */
3264 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3265 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3266 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3267 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3268 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003269
3270 /* No assignment, used for newer Lenovo models */
3271 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3272 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3273 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3274 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3275 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3276 KEY_UNKNOWN, KEY_UNKNOWN
3277
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003278 },
3279
3280 /* Generic keymap for Lenovo ThinkPads */
3281 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003282 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3283 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003284 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003285 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003286
3287 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003288 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3289 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3290 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003291
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003292 /* These should be enabled --only-- when ACPI video
3293 * is disabled (i.e. in "vendor" mode), and are handled
3294 * in a special way by the init code */
3295 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3296 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003297
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003298 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003299
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003300 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3301 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003302
3303 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3304 * react to it and reprograms the built-in *extra* mixer.
3305 * Never map it to control another mixer by default.
3306 *
3307 * T60?, T61, R60?, R61: firmware and EC tries to send
3308 * these over the regular keyboard, so these are no-ops,
3309 * but there are still weird bugs re. MUTE, so do not
3310 * change unless you get test reports from all Lenovo
3311 * models. May cause the BIOS to interfere with the
3312 * HDA mixer.
3313 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003314 KEY_RESERVED, /* 0x14: VOLUME UP */
3315 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3316 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003317
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003318 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003319
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003320 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003321 KEY_UNKNOWN, KEY_UNKNOWN,
3322
3323 /*
3324 * The mic mute button only sends 0x1a. It does not
3325 * automatically mute the mic or change the mute light.
3326 */
3327 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3328
3329 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003330 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003331
3332 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003333 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003334
3335 /*
3336 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3337 * The first item in this list is the Mute button which is
3338 * emitted with 0x103 through
3339 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3340 * symbol is held.
3341 * We'll need to offset those by 0x20.
3342 */
3343 KEY_RESERVED, /* Mute held, 0x103 */
3344 KEY_BRIGHTNESS_MIN, /* Backlight off */
3345 KEY_RESERVED, /* Clipping tool */
3346 KEY_RESERVED, /* Cloud */
3347 KEY_RESERVED,
3348 KEY_VOICECOMMAND, /* Voice */
3349 KEY_RESERVED,
3350 KEY_RESERVED, /* Gestures */
3351 KEY_RESERVED,
3352 KEY_RESERVED,
3353 KEY_RESERVED,
3354 KEY_CONFIG, /* Settings */
3355 KEY_RESERVED, /* New tab */
3356 KEY_REFRESH, /* Reload */
3357 KEY_BACK, /* Back */
3358 KEY_RESERVED, /* Microphone down */
3359 KEY_RESERVED, /* Microphone up */
3360 KEY_RESERVED, /* Microphone cancellation */
3361 KEY_RESERVED, /* Camera mode */
3362 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003363
3364 /*
3365 * These are found in 2017 models (e.g. T470s, X270).
3366 * The lowest known value is 0x311, which according to
3367 * the manual should launch a user defined favorite
3368 * application.
3369 *
3370 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3371 * corresponding to 0x34.
3372 */
3373
3374 /* (assignments unknown, please report if found) */
3375 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3376 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3377 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3378 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3379 KEY_UNKNOWN,
3380
Hans de Goede7ed77482020-09-08 12:21:11 -07003381 KEY_BOOKMARKS, /* Favorite app, 0x311 */
Hans de Goedee2c8c4e2020-09-08 12:21:50 -07003382 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
Hans de Goede7ed77482020-09-08 12:21:11 -07003383 KEY_CALC, /* Calculator (above numpad, P52) */
3384 KEY_BLUETOOTH, /* Bluetooth */
3385 KEY_KEYBOARD, /* Keyboard, 0x315 */
3386 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3387 KEY_NOTIFICATION_CENTER, /* Notification Center */
3388 KEY_PICKUP_PHONE, /* Answer incoming call */
3389 KEY_HANGUP_PHONE, /* Decline incoming call */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003390 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003391 };
3392
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003393 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3394 /* Generic maps (fallback) */
3395 {
3396 .vendor = PCI_VENDOR_ID_IBM,
3397 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3398 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3399 },
3400 {
3401 .vendor = PCI_VENDOR_ID_LENOVO,
3402 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3403 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3404 },
3405 };
3406
3407#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003408#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003409
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003410 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003411 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003412 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003413 bool radiosw_state = false;
3414 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003415
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003416 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003417 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003418
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003419 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003421
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003422 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003423 BUG_ON(tpacpi_inputdev->open != NULL ||
3424 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003425
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003426 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003427 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003428
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003429#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003430 mutex_init(&hotkey_thread_data_mutex);
3431#endif
3432
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003433 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003434 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003435
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003436 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3437 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003438 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003439
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003440 if (!tp_features.hotkey)
3441 return 1;
3442
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003443 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3444 ARRAY_SIZE(tpacpi_hotkey_qtable));
3445
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003446 tpacpi_disable_brightness_delay();
3447
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003448 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003449 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3450 for HKEY interface version 0x100 */
3451 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003452 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3453 "firmware HKEY interface version: 0x%x\n",
3454 hkeyv);
3455
3456 switch (hkeyv >> 8) {
3457 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003458 /*
3459 * MHKV 0x100 in A31, R40, R40e,
3460 * T4x, X31, and later
3461 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003462
3463 /* Paranoia check AND init hotkey_all_mask */
3464 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3465 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003466 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003467 TPACPI_MAIL);
3468 /* Fallback: pre-init for FN+F3,F4,F12 */
3469 hotkey_all_mask = 0x080cU;
3470 } else {
3471 tp_features.hotkey_mask = 1;
3472 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003473 break;
3474
3475 case 2:
3476 /*
3477 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3478 */
3479
3480 /* Paranoia check AND init hotkey_all_mask */
3481 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3482 "MHKA", "dd", 1)) {
3483 pr_err("missing MHKA handler, please report this to %s\n",
3484 TPACPI_MAIL);
3485 /* Fallback: pre-init for FN+F3,F4,F12 */
3486 hotkey_all_mask = 0x080cU;
3487 } else {
3488 tp_features.hotkey_mask = 1;
3489 }
3490
3491 /*
3492 * Check if we have an adaptive keyboard, like on the
3493 * Lenovo Carbon X1 2014 (2nd Gen).
3494 */
3495 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3496 "MHKA", "dd", 2)) {
3497 if (hotkey_adaptive_all_mask != 0) {
3498 tp_features.has_adaptive_kbd = true;
3499 res = sysfs_create_group(
3500 &tpacpi_pdev->dev.kobj,
3501 &adaptive_kbd_attr_group);
3502 if (res)
3503 goto err_exit;
3504 }
3505 } else {
3506 tp_features.has_adaptive_kbd = false;
3507 hotkey_adaptive_all_mask = 0x0U;
3508 }
3509 break;
3510
3511 default:
3512 pr_err("unknown version of the HKEY interface: 0x%x\n",
3513 hkeyv);
3514 pr_err("please report this to %s\n", TPACPI_MAIL);
3515 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003516 }
3517 }
3518
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003519 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3520 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003521 str_supported(tp_features.hotkey_mask));
3522
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003523 /* Init hotkey_all_mask if not initialized yet */
3524 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3525 (quirks & TPACPI_HK_Q_INIMASK))
3526 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003527
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003528 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003529 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003530 /* hotkey_source_mask *must* be zero for
3531 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003532 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003533 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003534 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003535
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003536 hotkey_orig_mask = hotkey_acpi_mask;
3537 } else {
3538 hotkey_orig_mask = hotkey_all_mask;
3539 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003540 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003541
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003542#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3543 if (dbg_wlswemul) {
3544 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003545 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003546 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003547 } else
3548#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003549 /* Not all thinkpads have a hardware radio switch */
3550 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3551 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003552 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003553 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003554 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003555 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003556
Len Bakerc99ca782021-09-26 13:19:08 +02003557 tabletsw_state = hotkey_init_tablet_mode();
3558 res = sysfs_create_group(&tpacpi_pdev->dev.kobj, &hotkey_attr_group);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003559 if (res)
3560 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003561
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003562 /* Set up key map */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003563 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3564 ARRAY_SIZE(tpacpi_keymap_qtable));
3565 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3566 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3567 "using keymap number %lu\n", keymap_id);
3568
Fuqian Huang35b7c802019-07-04 00:30:00 +08003569 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3570 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3571 if (!hotkey_keycode_map) {
3572 pr_err("failed to allocate memory for key map\n");
3573 res = -ENOMEM;
3574 goto err_exit;
3575 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003576
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003577 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003578 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3579 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3580 tpacpi_inputdev->keycode = hotkey_keycode_map;
3581 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3582 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003583 input_set_capability(tpacpi_inputdev, EV_KEY,
3584 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003585 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003586 if (i < sizeof(hotkey_reserved_mask)*8)
3587 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003588 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003589 }
3590
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003591 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003592 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003593 input_report_switch(tpacpi_inputdev,
3594 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003595 }
3596 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003597 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003598 input_report_switch(tpacpi_inputdev,
3599 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003600 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003601
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003602 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003603 * userspace. tpacpi_detect_brightness_capabilities() must have
3604 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003605 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003606 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3607 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003608
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003609 /* Disable brightness up/down on Lenovo thinkpads when
3610 * ACPI is handling them, otherwise it is plain impossible
3611 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003612 hotkey_reserved_mask |=
3613 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3614 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003615 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3616 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003617 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003618
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003619#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003620 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3621 & ~hotkey_all_mask
3622 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003623
3624 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3625 "hotkey source mask 0x%08x, polling freq %u\n",
3626 hotkey_source_mask, hotkey_poll_freq);
3627#endif
3628
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003629 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3630 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003631 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003632 if (res) {
3633 hotkey_exit();
3634 return res;
3635 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003636 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3637 | hotkey_driver_mask)
3638 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003639 if (res < 0 && res != -ENXIO) {
3640 hotkey_exit();
3641 return res;
3642 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003643 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3644 & ~hotkey_reserved_mask;
3645 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3646 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3647 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003648
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003649 tpacpi_inputdev->open = &hotkey_inputdev_open;
3650 tpacpi_inputdev->close = &hotkey_inputdev_close;
3651
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003652 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003653
3654 return 0;
3655
3656err_exit:
Len Bakerc99ca782021-09-26 13:19:08 +02003657 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &hotkey_attr_group);
3658 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &adaptive_kbd_attr_group);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003659
Jan van den Berg72a979f2014-09-17 00:01:08 +02003660 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003661}
3662
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003663/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3664 * mode, Web conference mode, Function mode and Lay-flat mode.
3665 * We support Home mode and Function mode currently.
3666 *
3667 * Will consider support rest of modes in future.
3668 *
3669 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003670static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003671 HOME_MODE,
3672/* WEB_BROWSER_MODE = 2,
3673 WEB_CONFERENCE_MODE = 3, */
3674 FUNCTION_MODE
3675};
3676
3677#define DFR_CHANGE_ROW 0x101
3678#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003679#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003680
3681/* press Fn key a while second, it will switch to Function Mode. Then
3682 * release Fn key, previous mode be restored.
3683 */
3684static bool adaptive_keyboard_mode_is_saved;
3685static int adaptive_keyboard_prev_mode;
3686
Bastien Noceraf74587f2015-03-02 14:45:22 +01003687static int adaptive_keyboard_get_mode(void)
3688{
3689 int mode = 0;
3690
3691 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3692 pr_err("Cannot read adaptive keyboard mode\n");
3693 return -EIO;
3694 }
3695
3696 return mode;
3697}
3698
3699static int adaptive_keyboard_set_mode(int new_mode)
3700{
3701 if (new_mode < 0 ||
3702 new_mode > LAYFLAT_MODE)
3703 return -EINVAL;
3704
3705 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3706 pr_err("Cannot set adaptive keyboard mode\n");
3707 return -EIO;
3708 }
3709
3710 return 0;
3711}
3712
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003713static int adaptive_keyboard_get_next_mode(int mode)
3714{
3715 size_t i;
3716 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3717
3718 for (i = 0; i <= max_mode; i++) {
3719 if (adaptive_keyboard_modes[i] == mode)
3720 break;
3721 }
3722
3723 if (i >= max_mode)
3724 i = 0;
3725 else
3726 i++;
3727
3728 return adaptive_keyboard_modes[i];
3729}
3730
3731static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3732{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003733 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003734 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003735 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003736
3737 switch (scancode) {
3738 case DFR_CHANGE_ROW:
3739 if (adaptive_keyboard_mode_is_saved) {
3740 new_mode = adaptive_keyboard_prev_mode;
3741 adaptive_keyboard_mode_is_saved = false;
3742 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003743 current_mode = adaptive_keyboard_get_mode();
3744 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003745 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003746 new_mode = adaptive_keyboard_get_next_mode(
3747 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003748 }
3749
Bastien Noceraf74587f2015-03-02 14:45:22 +01003750 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003751 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003752
3753 return true;
3754
3755 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003756 current_mode = adaptive_keyboard_get_mode();
3757 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003758 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003759
Bastien Noceraf74587f2015-03-02 14:45:22 +01003760 adaptive_keyboard_prev_mode = current_mode;
3761 adaptive_keyboard_mode_is_saved = true;
3762
3763 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3764 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003765 return true;
3766
3767 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003768 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003769 scancode >= FIRST_ADAPTIVE_KEY +
3770 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3771 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003772 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003773 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003774 return false;
3775 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003776 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3777 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003778 if (keycode != KEY_RESERVED) {
3779 mutex_lock(&tpacpi_inputdev_send_mutex);
3780
3781 input_report_key(tpacpi_inputdev, keycode, 1);
3782 input_sync(tpacpi_inputdev);
3783
3784 input_report_key(tpacpi_inputdev, keycode, 0);
3785 input_sync(tpacpi_inputdev);
3786
3787 mutex_unlock(&tpacpi_inputdev_send_mutex);
3788 }
3789 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003790 }
3791}
3792
Hans de Goede1b8101d2021-10-05 22:23:18 +02003793static bool hotkey_notify_extended_hotkey(const u32 hkey)
3794{
3795 unsigned int scancode;
3796
Hans de Goedef8098912021-10-05 22:23:20 +02003797 switch (hkey) {
3798 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
3799 tpacpi_driver_event(hkey);
3800 return true;
3801 }
3802
Hans de Goede1b8101d2021-10-05 22:23:18 +02003803 /* Extended keycodes start at 0x300 and our offset into the map
3804 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3805 * will be positive, but might not be in the correct range.
3806 */
3807 scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3808 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3809 scancode < TPACPI_HOTKEY_MAP_LEN) {
3810 tpacpi_input_send_key(scancode);
3811 return true;
3812 }
3813
3814 return false;
3815}
3816
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003817static bool hotkey_notify_hotkey(const u32 hkey,
3818 bool *send_acpi_ev,
3819 bool *ignore_acpi_ev)
3820{
3821 /* 0x1000-0x1FFF: key presses */
3822 unsigned int scancode = hkey & 0xfff;
3823 *send_acpi_ev = true;
3824 *ignore_acpi_ev = false;
3825
Christian Kellner696c65232017-02-28 17:10:57 +01003826 /*
3827 * Original events are in the 0x10XX range, the adaptive keyboard
3828 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3829 * models, additional keys are emitted through 0x13XX.
3830 */
3831 switch ((hkey >> 8) & 0xf) {
3832 case 0:
3833 if (scancode > 0 &&
3834 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3835 /* HKEY event 0x1001 is scancode 0x00 */
3836 scancode--;
3837 if (!(hotkey_source_mask & (1 << scancode))) {
3838 tpacpi_input_send_key_masked(scancode);
3839 *send_acpi_ev = false;
3840 } else {
3841 *ignore_acpi_ev = true;
3842 }
3843 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003844 }
Christian Kellner696c65232017-02-28 17:10:57 +01003845 break;
3846
3847 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003848 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003849
3850 case 3:
Hans de Goede1b8101d2021-10-05 22:23:18 +02003851 return hotkey_notify_extended_hotkey(hkey);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003852 }
Christian Kellner696c65232017-02-28 17:10:57 +01003853
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003854 return false;
3855}
3856
3857static bool hotkey_notify_wakeup(const u32 hkey,
3858 bool *send_acpi_ev,
3859 bool *ignore_acpi_ev)
3860{
3861 /* 0x2000-0x2FFF: Wakeup reason */
3862 *send_acpi_ev = true;
3863 *ignore_acpi_ev = false;
3864
3865 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003866 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3867 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003868 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3869 *ignore_acpi_ev = true;
3870 break;
3871
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003872 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3873 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003874 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3875 *ignore_acpi_ev = true;
3876 break;
3877
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003878 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3879 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003880 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003881 /* how to auto-heal: */
3882 /* 2313: woke up from S3, go to S4/S5 */
3883 /* 2413: woke up from S4, go to S5 */
3884 break;
3885
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003886 default:
3887 return false;
3888 }
3889
3890 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003891 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003892 hotkey_wakeup_reason_notify_change();
3893 }
3894 return true;
3895}
3896
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003897static bool hotkey_notify_dockevent(const u32 hkey,
3898 bool *send_acpi_ev,
3899 bool *ignore_acpi_ev)
3900{
3901 /* 0x4000-0x4FFF: dock-related events */
3902 *send_acpi_ev = true;
3903 *ignore_acpi_ev = false;
3904
3905 switch (hkey) {
3906 case TP_HKEY_EV_UNDOCK_ACK:
3907 /* ACPI undock operation completed after wakeup */
3908 hotkey_autosleep_ack = 1;
3909 pr_info("undocked\n");
3910 hotkey_wakeup_hotunplug_complete_notify_change();
3911 return true;
3912
3913 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3914 pr_info("docked into hotplug port replicator\n");
3915 return true;
3916 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3917 pr_info("undocked from hotplug port replicator\n");
3918 return true;
3919
Alexander Kobel0a053f02021-02-13 16:13:36 +01003920 /*
3921 * Deliberately ignore attaching and detaching the keybord cover to avoid
3922 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3923 * to userspace.
3924 *
3925 * Please refer to the following thread for more information and a preliminary
3926 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3927 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3928 * the Pico cartridge dock module:
3929 * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
3930 */
3931 case TP_HKEY_EV_KBD_COVER_ATTACH:
3932 case TP_HKEY_EV_KBD_COVER_DETACH:
3933 *send_acpi_ev = false;
3934 *ignore_acpi_ev = true;
3935 return true;
3936
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003937 default:
3938 return false;
3939 }
3940}
3941
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003942static bool hotkey_notify_usrevent(const u32 hkey,
3943 bool *send_acpi_ev,
3944 bool *ignore_acpi_ev)
3945{
3946 /* 0x5000-0x5FFF: human interface helpers */
3947 *send_acpi_ev = true;
3948 *ignore_acpi_ev = false;
3949
3950 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003951 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3952 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003953 return true;
3954
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003955 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3956 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003957 tpacpi_input_send_tabletsw();
3958 hotkey_tablet_mode_notify_change();
3959 *send_acpi_ev = false;
3960 return true;
3961
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003962 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3963 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3964 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003965 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003966 *ignore_acpi_ev = true;
3967 return true;
3968
3969 default:
3970 return false;
3971 }
3972}
3973
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003974static void thermal_dump_all_sensors(void);
Mark Pearson1ac09652020-11-24 13:11:54 -05003975static void palmsensor_refresh(void);
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003976
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003977static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003978 bool *send_acpi_ev,
3979 bool *ignore_acpi_ev)
3980{
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003981 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003982 *send_acpi_ev = true;
3983 *ignore_acpi_ev = false;
3984
3985 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003986 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -03003987 pr_debug("EC reports: Thermal Table has changed\n");
3988 /* recommended action: do nothing, we don't have
3989 * Lenovo ATM information */
3990 return true;
3991 case TP_HKEY_EV_THM_CSM_COMPLETED:
3992 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04003993 /* Thermal event - pass on to event handler */
3994 tpacpi_driver_event(hkey);
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -03003995 return true;
3996 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3997 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003998 /* recommended action: do nothing, we don't have
3999 * Lenovo ATM information */
4000 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004001 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07004002 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004003 /* recommended action: warn user through gui */
4004 break;
4005 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07004006 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004007 /* recommended action: immediate sleep/hibernate */
4008 break;
4009 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004010 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004011 /* recommended action: warn user through gui, that */
4012 /* some internal component is too hot */
4013 break;
4014 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004015 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004016 /* recommended action: immediate sleep/hibernate */
4017 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01004018 case TP_HKEY_EV_AC_CHANGED:
4019 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4020 * AC status changed; can be triggered by plugging or
4021 * unplugging AC adapter, docking or undocking. */
4022
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004023 fallthrough;
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004024
4025 case TP_HKEY_EV_KEY_NUMLOCK:
4026 case TP_HKEY_EV_KEY_FN:
4027 /* key press events, we just ignore them as long as the EC
4028 * is still reporting them in the normal keyboard stream */
4029 *send_acpi_ev = false;
4030 *ignore_acpi_ev = true;
4031 return true;
4032
Esteve Varela Colominas12dde4d2021-03-15 20:58:24 +01004033 case TP_HKEY_EV_KEY_FN_ESC:
Esteve Varela Colominas49702ba2021-03-21 19:35:13 +01004034 /* Get the media key status to force the status LED to update */
Esteve Varela Colominas12dde4d2021-03-15 20:58:24 +01004035 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
4036 *send_acpi_ev = false;
4037 *ignore_acpi_ev = true;
4038 return true;
4039
Lyudeb03f4d42016-11-11 15:15:03 -05004040 case TP_HKEY_EV_TABLET_CHANGED:
4041 tpacpi_input_send_tabletsw();
4042 hotkey_tablet_mode_notify_change();
4043 *send_acpi_ev = false;
Henrique de Moraes Holschuhedd1ed72018-04-24 16:56:04 -03004044 return true;
Lyudeb03f4d42016-11-11 15:15:03 -05004045
David Herrmann587d8622018-01-12 12:04:45 +01004046 case TP_HKEY_EV_PALM_DETECTED:
4047 case TP_HKEY_EV_PALM_UNDETECTED:
Mark Pearson1ac09652020-11-24 13:11:54 -05004048 /* palm detected - pass on to event handler */
4049 palmsensor_refresh();
David Herrmann587d8622018-01-12 12:04:45 +01004050 return true;
4051
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004052 default:
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004053 /* report simply as unknown, no sensor dump */
4054 return false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004055 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004056
4057 thermal_dump_all_sensors();
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004058 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004059}
4060
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004061static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4062{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004063 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004064 bool send_acpi_ev;
4065 bool ignore_acpi_ev;
4066 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004067
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004068 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004069 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004070 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004071 acpi_bus_generate_netlink_event(
4072 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004073 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004074 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004075 return;
4076 }
4077
4078 while (1) {
4079 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004080 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004081 return;
4082 }
4083
4084 if (hkey == 0) {
4085 /* queue empty */
4086 return;
4087 }
4088
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004089 send_acpi_ev = true;
4090 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004091
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004092 switch (hkey >> 12) {
4093 case 1:
4094 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004095 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4096 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004097 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004098 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004099 /* 0x2000-0x2FFF: Wakeup reason */
4100 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4101 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004102 break;
4103 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004104 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004105 switch (hkey) {
4106 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004107 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004108 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004109 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004110 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004111 break;
4112 case TP_HKEY_EV_OPTDRV_EJ:
4113 /* FIXME: kick libata if SATA link offline */
4114 known_ev = true;
4115 break;
4116 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004117 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004118 }
4119 break;
4120 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004121 /* 0x4000-0x4FFF: dock-related events */
4122 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4123 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004124 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004125 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004126 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004127 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4128 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004129 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004130 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004131 /* 0x6000-0x6FFF: thermal alarms/notices and
4132 * keyboard events */
4133 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004134 &ignore_acpi_ev);
4135 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004136 case 7:
4137 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004138 if (tp_features.hotkey_wlsw &&
4139 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004140 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004141 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004142 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004143 break;
4144 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004145 fallthrough; /* to default */
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004146 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004147 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004148 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004149 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004150 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004151 pr_notice("please report the conditions when this event happened to %s\n",
4152 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004153 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004154
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004155 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004156 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004157 acpi_bus_generate_netlink_event(
4158 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004159 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004160 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004161 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004162 }
4163}
4164
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004165static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004166{
4167 /* Do these on suspend, we get the events on early resume! */
4168 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4169 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004170
4171 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004172 if (tp_features.has_adaptive_kbd) {
4173 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4174 "GTRW", "dd", 0)) {
4175 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004176 }
4177 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004178}
4179
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004180static void hotkey_resume(void)
4181{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004182 tpacpi_disable_brightness_delay();
4183
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004184 if (hotkey_status_set(true) < 0 ||
4185 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004186 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004187
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004188 tpacpi_send_radiosw_update();
Benjamin Berg96adb412020-11-23 14:21:57 +01004189 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004190 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004191 hotkey_wakeup_reason_notify_change();
4192 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004193 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004194
4195 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004196 if (tp_features.has_adaptive_kbd) {
4197 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4198 adaptive_keyboard_prev_mode)) {
4199 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004200 }
4201 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004202}
4203
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004204/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004205static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004207 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004209 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004210 seq_printf(m, "status:\t\tnot supported\n");
4211 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004212 }
4213
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004214 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004215 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004216 res = hotkey_status_get(&status);
4217 if (!res)
4218 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004219 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004220 if (res)
4221 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004223 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004224 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004225 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4226 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004228 seq_printf(m, "mask:\t\tnot supported\n");
4229 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
4231
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233}
4234
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004235static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004236{
4237 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004238 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004239 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4240 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004241}
4242
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004243static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004245 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004246 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004249 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return -ENODEV;
4251
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004252 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004253 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004254
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004255 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004256
4257 res = 0;
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03004258 while ((cmd = strsep(&buf, ","))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004260 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004262 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004263 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004265 mask = (hotkey_all_mask | hotkey_source_mask)
4266 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4268 /* mask set */
4269 } else if (sscanf(cmd, "%x", &mask) == 1) {
4270 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004271 } else {
4272 res = -EINVAL;
4273 goto errexit;
4274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004276
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004277 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004278 tpacpi_disclose_usertask("procfs hotkey",
4279 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004280 res = hotkey_user_mask_set(mask);
4281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004283errexit:
4284 mutex_unlock(&hotkey_mutex);
4285 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004288static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004289 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4290 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004291 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004292 {"", 0},
4293};
4294
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004295static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004296 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004297 .notify = hotkey_notify,
4298 .handle = &hkey_handle,
4299 .type = ACPI_DEVICE_NOTIFY,
4300};
4301
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004302static struct ibm_struct hotkey_driver_data = {
4303 .name = "hotkey",
4304 .read = hotkey_read,
4305 .write = hotkey_write,
4306 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004307 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004308 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004309 .acpi = &ibm_hotkey_acpidriver,
4310};
4311
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004312/*************************************************************************
4313 * Bluetooth subdriver
4314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004316enum {
4317 /* ACPI GBDC/SBDC bits */
4318 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4319 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004320 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004321 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004322};
4323
4324enum {
4325 /* ACPI \BLTH commands */
4326 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4327 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4328 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4329 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4330 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004331};
4332
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004333#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4334
Johannes Berg19d337d2009-06-02 13:01:37 +02004335static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004336{
4337 int status;
4338
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004339#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4340 if (dbg_bluetoothemul)
4341 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004342 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004343#endif
4344
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004345 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4346 return -EIO;
4347
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004348 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004349 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004350}
4351
Johannes Berg19d337d2009-06-02 13:01:37 +02004352static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004353{
4354 int status;
4355
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004356 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004357 "will attempt to %s bluetooth\n",
4358 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004359
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004360#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4361 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004362 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004363 return 0;
4364 }
4365#endif
4366
Johannes Berg19d337d2009-06-02 13:01:37 +02004367 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004368 status = TP_ACPI_BLUETOOTH_RADIOSSW
4369 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4370 else
4371 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004372
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004373 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4374 return -EIO;
4375
4376 return 0;
4377}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004378
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004379/* sysfs bluetooth enable ---------------------------------------------- */
4380static ssize_t bluetooth_enable_show(struct device *dev,
4381 struct device_attribute *attr,
4382 char *buf)
4383{
Johannes Berg19d337d2009-06-02 13:01:37 +02004384 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4385 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004386}
4387
4388static ssize_t bluetooth_enable_store(struct device *dev,
4389 struct device_attribute *attr,
4390 const char *buf, size_t count)
4391{
Johannes Berg19d337d2009-06-02 13:01:37 +02004392 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4393 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004394}
4395
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004396static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004397
4398/* --------------------------------------------------------------------- */
4399
4400static struct attribute *bluetooth_attributes[] = {
4401 &dev_attr_bluetooth_enable.attr,
4402 NULL
4403};
4404
4405static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004406 .attrs = bluetooth_attributes,
4407};
4408
Johannes Berg19d337d2009-06-02 13:01:37 +02004409static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4410 .get_status = bluetooth_get_status,
4411 .set_status = bluetooth_set_status,
4412};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004413
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004414static void bluetooth_shutdown(void)
4415{
4416 /* Order firmware to save current state to NVRAM */
4417 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4418 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004419 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004420 else
4421 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004422 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004423}
4424
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004425static void bluetooth_exit(void)
4426{
4427 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4428 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004429
4430 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4431
4432 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004433}
4434
Jiaxun Yangf7db8392019-03-07 17:37:16 +08004435static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4436 {
4437 .ident = "ThinkPad E485",
4438 .matches = {
4439 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4440 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4441 },
4442 },
4443 {
4444 .ident = "ThinkPad E585",
4445 .matches = {
4446 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4447 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4448 },
4449 },
4450 {
4451 .ident = "ThinkPad A285 - 20MW",
4452 .matches = {
4453 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4454 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4455 },
4456 },
4457 {
4458 .ident = "ThinkPad A285 - 20MX",
4459 .matches = {
4460 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4461 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4462 },
4463 },
4464 {
4465 .ident = "ThinkPad A485 - 20MU",
4466 .matches = {
4467 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4468 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4469 },
4470 },
4471 {
4472 .ident = "ThinkPad A485 - 20MV",
4473 .matches = {
4474 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4475 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4476 },
4477 },
4478 {}
4479};
4480
4481static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4482 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4483 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4484 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4485 {}
4486};
4487
4488
4489static int __init have_bt_fwbug(void)
4490{
4491 /*
4492 * Some AMD based ThinkPads have a firmware bug that calling
4493 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4494 */
4495 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4496 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4497 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4498 return 1;
4499 } else
4500 return 0;
4501}
4502
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004503static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004505 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004506 int status = 0;
4507
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004508 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4509 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004510
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004511 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004512
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004513 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4514 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Jiaxun Yangf7db8392019-03-07 17:37:16 +08004515 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004516 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004518 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4519 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004520 str_supported(tp_features.bluetooth),
4521 status);
4522
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004523#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4524 if (dbg_bluetoothemul) {
4525 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004526 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004527 } else
4528#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004529 if (tp_features.bluetooth &&
4530 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4531 /* no bluetooth hardware present in system */
4532 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004533 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004534 "bluetooth hardware not installed\n");
4535 }
4536
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004537 if (!tp_features.bluetooth)
4538 return 1;
4539
Johannes Berg19d337d2009-06-02 13:01:37 +02004540 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4541 &bluetooth_tprfk_ops,
4542 RFKILL_TYPE_BLUETOOTH,
4543 TPACPI_RFK_BLUETOOTH_SW_NAME,
4544 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004545 if (res)
4546 return res;
4547
Johannes Berg19d337d2009-06-02 13:01:37 +02004548 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4549 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004550 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004551 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004552 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004553 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004554
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556}
4557
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004558/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004559static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004561 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004564static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Johannes Berg19d337d2009-06-02 13:01:37 +02004566 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004569static struct ibm_struct bluetooth_driver_data = {
4570 .name = "bluetooth",
4571 .read = bluetooth_read,
4572 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004573 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004574 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004575};
4576
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004577/*************************************************************************
4578 * Wan subdriver
4579 */
4580
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004581enum {
4582 /* ACPI GWAN/SWAN bits */
4583 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4584 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004585 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004586 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004587};
4588
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004589#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4590
Johannes Berg19d337d2009-06-02 13:01:37 +02004591static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004592{
4593 int status;
4594
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004595#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4596 if (dbg_wwanemul)
4597 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004598 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004599#endif
4600
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004601 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4602 return -EIO;
4603
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004604 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004605 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004606}
4607
Johannes Berg19d337d2009-06-02 13:01:37 +02004608static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004609{
4610 int status;
4611
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004612 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004613 "will attempt to %s wwan\n",
4614 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004615
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004616#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4617 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004618 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004619 return 0;
4620 }
4621#endif
4622
Johannes Berg19d337d2009-06-02 13:01:37 +02004623 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004624 status = TP_ACPI_WANCARD_RADIOSSW
4625 | TP_ACPI_WANCARD_RESUMECTRL;
4626 else
4627 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004628
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004629 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4630 return -EIO;
4631
4632 return 0;
4633}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004634
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004635/* sysfs wan enable ---------------------------------------------------- */
4636static ssize_t wan_enable_show(struct device *dev,
4637 struct device_attribute *attr,
4638 char *buf)
4639{
Johannes Berg19d337d2009-06-02 13:01:37 +02004640 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4641 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004642}
4643
4644static ssize_t wan_enable_store(struct device *dev,
4645 struct device_attribute *attr,
4646 const char *buf, size_t count)
4647{
Johannes Berg19d337d2009-06-02 13:01:37 +02004648 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4649 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004650}
4651
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004652static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4653 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004654
4655/* --------------------------------------------------------------------- */
4656
4657static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004658 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004659 NULL
4660};
4661
4662static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004663 .attrs = wan_attributes,
4664};
4665
Johannes Berg19d337d2009-06-02 13:01:37 +02004666static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4667 .get_status = wan_get_status,
4668 .set_status = wan_set_status,
4669};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004670
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004671static void wan_shutdown(void)
4672{
4673 /* Order firmware to save current state to NVRAM */
4674 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4675 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004676 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004677 else
4678 vdbg_printk(TPACPI_DBG_RFKILL,
4679 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004680}
4681
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004682static void wan_exit(void)
4683{
4684 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4685 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004686
4687 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4688
4689 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004690}
4691
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004692static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004693{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004694 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004695 int status = 0;
4696
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004697 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4698 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004699
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004700 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004701
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004702 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004703 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004704
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004705 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4706 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004707 str_supported(tp_features.wan),
4708 status);
4709
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004710#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4711 if (dbg_wwanemul) {
4712 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004713 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004714 } else
4715#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004716 if (tp_features.wan &&
4717 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4718 /* no wan hardware present in system */
4719 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004720 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004721 "wan hardware not installed\n");
4722 }
4723
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004724 if (!tp_features.wan)
4725 return 1;
4726
Johannes Berg19d337d2009-06-02 13:01:37 +02004727 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4728 &wan_tprfk_ops,
4729 RFKILL_TYPE_WWAN,
4730 TPACPI_RFK_WWAN_SW_NAME,
4731 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004732 if (res)
4733 return res;
4734
Johannes Berg19d337d2009-06-02 13:01:37 +02004735 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4736 &wan_attr_group);
4737
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004738 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004739 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004740 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004741 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004742
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004743 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004744}
4745
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004746/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004747static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004748{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004749 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004750}
4751
4752static int wan_write(char *buf)
4753{
Johannes Berg19d337d2009-06-02 13:01:37 +02004754 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004755}
4756
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004757static struct ibm_struct wan_driver_data = {
4758 .name = "wan",
4759 .read = wan_read,
4760 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004761 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004762 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004763};
4764
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004765/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004766 * UWB subdriver
4767 */
4768
4769enum {
4770 /* ACPI GUWB/SUWB bits */
4771 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4772 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4773};
4774
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004775#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4776
Johannes Berg19d337d2009-06-02 13:01:37 +02004777static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004778{
4779 int status;
4780
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004781#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4782 if (dbg_uwbemul)
4783 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004784 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004785#endif
4786
4787 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4788 return -EIO;
4789
4790 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004791 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004792}
4793
Johannes Berg19d337d2009-06-02 13:01:37 +02004794static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004795{
4796 int status;
4797
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004798 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004799 "will attempt to %s UWB\n",
4800 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004801
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004802#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4803 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004804 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004805 return 0;
4806 }
4807#endif
4808
Johannes Berg19d337d2009-06-02 13:01:37 +02004809 if (state == TPACPI_RFK_RADIO_ON)
4810 status = TP_ACPI_UWB_RADIOSSW;
4811 else
4812 status = 0;
4813
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004814 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4815 return -EIO;
4816
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004817 return 0;
4818}
4819
4820/* --------------------------------------------------------------------- */
4821
Johannes Berg19d337d2009-06-02 13:01:37 +02004822static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4823 .get_status = uwb_get_status,
4824 .set_status = uwb_set_status,
4825};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004826
4827static void uwb_exit(void)
4828{
Johannes Berg19d337d2009-06-02 13:01:37 +02004829 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004830}
4831
4832static int __init uwb_init(struct ibm_init_struct *iibm)
4833{
4834 int res;
4835 int status = 0;
4836
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004837 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4838 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004839
4840 TPACPI_ACPIHANDLE_INIT(hkey);
4841
4842 tp_features.uwb = hkey_handle &&
4843 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4844
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004845 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4846 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004847 str_supported(tp_features.uwb),
4848 status);
4849
4850#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4851 if (dbg_uwbemul) {
4852 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004853 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004854 } else
4855#endif
4856 if (tp_features.uwb &&
4857 !(status & TP_ACPI_UWB_HWPRESENT)) {
4858 /* no uwb hardware present in system */
4859 tp_features.uwb = 0;
4860 dbg_printk(TPACPI_DBG_INIT,
4861 "uwb hardware not installed\n");
4862 }
4863
4864 if (!tp_features.uwb)
4865 return 1;
4866
4867 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004868 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004869 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004870 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004871 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004872 return res;
4873}
4874
4875static struct ibm_struct uwb_driver_data = {
4876 .name = "uwb",
4877 .exit = uwb_exit,
4878 .flags.experimental = 1,
4879};
4880
4881/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004882 * Video subdriver
4883 */
4884
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004885#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4886
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004887enum video_access_mode {
4888 TPACPI_VIDEO_NONE = 0,
4889 TPACPI_VIDEO_570, /* 570 */
4890 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4891 TPACPI_VIDEO_NEW, /* all others */
4892};
4893
4894enum { /* video status flags, based on VIDEO_570 */
4895 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4896 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4897 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4898};
4899
4900enum { /* TPACPI_VIDEO_570 constants */
4901 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4902 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4903 * video_status_flags */
4904 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4905 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4906};
4907
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004908static enum video_access_mode video_supported;
4909static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004910
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004911static int video_autosw_get(void);
4912static int video_autosw_set(int enable);
4913
Joe Perches112a6ee2011-04-04 10:06:24 -07004914TPACPI_HANDLE(vid, root,
4915 "\\_SB.PCI.AGP.VGA", /* 570 */
4916 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4917 "\\_SB.PCI0.VID0", /* 770e */
4918 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4919 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4920 "\\_SB.PCI0.AGP.VID", /* all others */
4921 ); /* R30, R31 */
4922
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004923TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004924
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004925static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004927 int ivga;
4928
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004929 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4930
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004931 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004932 if (tpacpi_is_ibm())
4933 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004934
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004935 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4936 /* G41, assume IVGA doesn't change */
4937 vid_handle = vid2_handle;
4938
4939 if (!vid_handle)
4940 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004941 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004942 else if (tpacpi_is_ibm() &&
4943 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004945 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004946 else if (tpacpi_is_ibm() &&
4947 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004948 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004949 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004950 else
4951 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004952 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004954 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4955 str_supported(video_supported != TPACPI_VIDEO_NONE),
4956 video_supported);
4957
Jan van den Berg72a979f2014-09-17 00:01:08 +02004958 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004961static void video_exit(void)
4962{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004963 dbg_printk(TPACPI_DBG_EXIT,
4964 "restoring original video autoswitch mode\n");
4965 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004966 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004967}
4968
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004969static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970{
4971 int status = 0;
4972 int i;
4973
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004974 switch (video_supported) {
4975 case TPACPI_VIDEO_570:
4976 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4977 TP_ACPI_VIDEO_570_PHSCMD))
4978 return -EIO;
4979 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4980 break;
4981 case TPACPI_VIDEO_770:
4982 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4983 return -EIO;
4984 if (i)
4985 status |= TP_ACPI_VIDEO_S_LCD;
4986 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4987 return -EIO;
4988 if (i)
4989 status |= TP_ACPI_VIDEO_S_CRT;
4990 break;
4991 case TPACPI_VIDEO_NEW:
4992 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4993 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4994 return -EIO;
4995 if (i)
4996 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004998 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4999 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5000 return -EIO;
5001 if (i)
5002 status |= TP_ACPI_VIDEO_S_LCD;
5003 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5004 return -EIO;
5005 if (i)
5006 status |= TP_ACPI_VIDEO_S_DVI;
5007 break;
5008 default:
5009 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
5012 return status;
5013}
5014
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005015static int video_outputsw_set(int status)
5016{
5017 int autosw;
5018 int res = 0;
5019
5020 switch (video_supported) {
5021 case TPACPI_VIDEO_570:
5022 res = acpi_evalf(NULL, NULL,
5023 "\\_SB.PHS2", "vdd",
5024 TP_ACPI_VIDEO_570_PHS2CMD,
5025 status | TP_ACPI_VIDEO_570_PHS2SET);
5026 break;
5027 case TPACPI_VIDEO_770:
5028 autosw = video_autosw_get();
5029 if (autosw < 0)
5030 return autosw;
5031
5032 res = video_autosw_set(1);
5033 if (res)
5034 return res;
5035 res = acpi_evalf(vid_handle, NULL,
5036 "ASWT", "vdd", status * 0x100, 0);
5037 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005038 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005039 return -EIO;
5040 }
5041 break;
5042 case TPACPI_VIDEO_NEW:
5043 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005044 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005045 break;
5046 default:
5047 return -ENOSYS;
5048 }
5049
Jan van den Berg72a979f2014-09-17 00:01:08 +02005050 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005051}
5052
5053static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005055 int autosw = 0;
5056
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005057 switch (video_supported) {
5058 case TPACPI_VIDEO_570:
5059 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5060 return -EIO;
5061 break;
5062 case TPACPI_VIDEO_770:
5063 case TPACPI_VIDEO_NEW:
5064 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5065 return -EIO;
5066 break;
5067 default:
5068 return -ENOSYS;
5069 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005070
5071 return autosw & 1;
5072}
5073
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005074static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005075{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005076 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005077 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005078 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005079}
5080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005081static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005082{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005083 int autosw = video_autosw_get();
5084 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005085
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005086 if (autosw < 0)
5087 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005088
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005089 switch (video_supported) {
5090 case TPACPI_VIDEO_570:
5091 res = video_autosw_set(1);
5092 if (res)
5093 return res;
5094 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5095 break;
5096 case TPACPI_VIDEO_770:
5097 case TPACPI_VIDEO_NEW:
5098 res = video_autosw_set(1);
5099 if (res)
5100 return res;
5101 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5102 break;
5103 default:
5104 return -ENOSYS;
5105 }
5106 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005107 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005108 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005109 }
5110
Jan van den Berg72a979f2014-09-17 00:01:08 +02005111 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005112}
5113
5114static int video_expand_toggle(void)
5115{
5116 switch (video_supported) {
5117 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005118 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005119 0 : -EIO;
5120 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005121 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005122 0 : -EIO;
5123 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005124 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005125 0 : -EIO;
5126 default:
5127 return -ENOSYS;
5128 }
5129 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005130}
5131
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005132static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005133{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005134 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005135
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005136 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005137 seq_printf(m, "status:\t\tnot supported\n");
5138 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005139 }
5140
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005141 /* Even reads can crash X.org, so... */
5142 if (!capable(CAP_SYS_ADMIN))
5143 return -EPERM;
5144
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005145 status = video_outputsw_get();
5146 if (status < 0)
5147 return status;
5148
5149 autosw = video_autosw_get();
5150 if (autosw < 0)
5151 return autosw;
5152
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005153 seq_printf(m, "status:\t\tsupported\n");
5154 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5155 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005156 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005157 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5158 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5159 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5160 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005161 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005162 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5163 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5164 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005165
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005166 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005167}
5168
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005169static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
5171 char *cmd;
5172 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005173 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005175 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005176 return -ENODEV;
5177
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005178 /* Even reads can crash X.org, let alone writes... */
5179 if (!capable(CAP_SYS_ADMIN))
5180 return -EPERM;
5181
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005182 enable = 0;
5183 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005185 while ((cmd = strsep(&buf, ","))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005187 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005189 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005191 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005193 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005194 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005195 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005196 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005197 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005198 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005199 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005201 res = video_autosw_set(1);
5202 if (res)
5203 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005205 res = video_autosw_set(0);
5206 if (res)
5207 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005209 res = video_outputsw_cycle();
5210 if (res)
5211 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005213 res = video_expand_toggle();
5214 if (res)
5215 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 } else
5217 return -EINVAL;
5218 }
5219
5220 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005221 status = video_outputsw_get();
5222 if (status < 0)
5223 return status;
5224 res = video_outputsw_set((status & ~disable) | enable);
5225 if (res)
5226 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 }
5228
5229 return 0;
5230}
5231
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005232static struct ibm_struct video_driver_data = {
5233 .name = "video",
5234 .read = video_read,
5235 .write = video_write,
5236 .exit = video_exit,
5237};
5238
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005239#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5240
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005241/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005242 * Keyboard backlight subdriver
5243 */
5244
Hans de Goedec685e202017-02-09 16:44:13 +01005245static enum led_brightness kbdlight_brightness;
5246static DEFINE_MUTEX(kbdlight_mutex);
5247
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005248static int kbdlight_set_level(int level)
5249{
Hans de Goedec685e202017-02-09 16:44:13 +01005250 int ret = 0;
5251
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005252 if (!hkey_handle)
5253 return -ENXIO;
5254
Hans de Goedec685e202017-02-09 16:44:13 +01005255 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005256
Hans de Goedec685e202017-02-09 16:44:13 +01005257 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5258 ret = -EIO;
5259 else
5260 kbdlight_brightness = level;
5261
5262 mutex_unlock(&kbdlight_mutex);
5263
5264 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005265}
5266
5267static int kbdlight_get_level(void)
5268{
5269 int status = 0;
5270
5271 if (!hkey_handle)
5272 return -ENXIO;
5273
5274 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5275 return -EIO;
5276
5277 if (status < 0)
5278 return status;
5279
5280 return status & 0x3;
5281}
5282
5283static bool kbdlight_is_supported(void)
5284{
5285 int status = 0;
5286
5287 if (!hkey_handle)
5288 return false;
5289
5290 if (!acpi_has_method(hkey_handle, "MLCG")) {
5291 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5292 return false;
5293 }
5294
5295 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5296 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5297 return false;
5298 }
5299
5300 if (status < 0) {
5301 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5302 return false;
5303 }
5304
5305 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5306 /*
5307 * Guessed test for keyboard backlight:
5308 *
5309 * Machines with backlight keyboard return:
5310 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5311 * b110100010010000000XX - ThinkPad x230
5312 * b010100000010000000XX - ThinkPad x240
5313 * b010100000010000000XX - ThinkPad W541
5314 * (XX is current backlight level)
5315 *
5316 * Machines without backlight keyboard return:
5317 * b10100001000000000000 - ThinkPad x230
5318 * b10110001000000000000 - ThinkPad E430
5319 * b00000000000000000000 - ThinkPad E450
5320 *
5321 * Candidate BITs for detection test (XOR):
5322 * b01000000001000000000
5323 * ^
5324 */
5325 return status & BIT(9);
5326}
5327
Hans de Goede86ec0c22017-02-09 16:44:12 +01005328static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005329 enum led_brightness brightness)
5330{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005331 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005332}
5333
5334static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5335{
5336 int level;
5337
5338 level = kbdlight_get_level();
5339 if (level < 0)
5340 return 0;
5341
5342 return level;
5343}
5344
5345static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5346 .led_classdev = {
5347 .name = "tpacpi::kbd_backlight",
5348 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005349 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005350 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005351 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005352 }
5353};
5354
5355static int __init kbdlight_init(struct ibm_init_struct *iibm)
5356{
5357 int rc;
5358
5359 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5360
5361 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005362
5363 if (!kbdlight_is_supported()) {
5364 tp_features.kbdlight = 0;
5365 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5366 return 1;
5367 }
5368
Hans de Goedec685e202017-02-09 16:44:13 +01005369 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005370 tp_features.kbdlight = 1;
5371
5372 rc = led_classdev_register(&tpacpi_pdev->dev,
5373 &tpacpi_led_kbdlight.led_classdev);
5374 if (rc < 0) {
5375 tp_features.kbdlight = 0;
5376 return rc;
5377 }
5378
Hans de Goedec685e202017-02-09 16:44:13 +01005379 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5380 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005381 return 0;
5382}
5383
5384static void kbdlight_exit(void)
5385{
Andy Shevchenko41f80042020-05-07 19:37:23 +03005386 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005387}
5388
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005389static int kbdlight_set_level_and_update(int level)
5390{
5391 int ret;
5392 struct led_classdev *led_cdev;
5393
5394 ret = kbdlight_set_level(level);
5395 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5396
5397 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5398 led_cdev->brightness = level;
5399
5400 return ret;
5401}
5402
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005403static int kbdlight_read(struct seq_file *m)
5404{
5405 int level;
5406
5407 if (!tp_features.kbdlight) {
5408 seq_printf(m, "status:\t\tnot supported\n");
5409 } else {
5410 level = kbdlight_get_level();
5411 if (level < 0)
5412 seq_printf(m, "status:\t\terror %d\n", level);
5413 else
5414 seq_printf(m, "status:\t\t%d\n", level);
5415 seq_printf(m, "commands:\t0, 1, 2\n");
5416 }
5417
5418 return 0;
5419}
5420
5421static int kbdlight_write(char *buf)
5422{
5423 char *cmd;
Andy Shevchenko466f4692020-05-11 15:54:14 +03005424 int res, level = -EINVAL;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005425
5426 if (!tp_features.kbdlight)
5427 return -ENODEV;
5428
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005429 while ((cmd = strsep(&buf, ","))) {
Andy Shevchenko466f4692020-05-11 15:54:14 +03005430 res = kstrtoint(cmd, 10, &level);
5431 if (res < 0)
5432 return res;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005433 }
5434
Andy Shevchenko466f4692020-05-11 15:54:14 +03005435 if (level >= 3 || level < 0)
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005436 return -EINVAL;
5437
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005438 return kbdlight_set_level_and_update(level);
5439}
5440
5441static void kbdlight_suspend(void)
5442{
5443 struct led_classdev *led_cdev;
5444
5445 if (!tp_features.kbdlight)
5446 return;
5447
5448 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5449 led_update_brightness(led_cdev);
5450 led_classdev_suspend(led_cdev);
5451}
5452
5453static void kbdlight_resume(void)
5454{
5455 if (!tp_features.kbdlight)
5456 return;
5457
5458 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005459}
5460
5461static struct ibm_struct kbdlight_driver_data = {
5462 .name = "kbdlight",
5463 .read = kbdlight_read,
5464 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005465 .suspend = kbdlight_suspend,
5466 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005467 .exit = kbdlight_exit,
5468};
5469
5470/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005471 * Light (thinklight) subdriver
5472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005474TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5475TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005476
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005477static int light_get_status(void)
5478{
5479 int status = 0;
5480
5481 if (tp_features.light_status) {
5482 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5483 return -EIO;
5484 return (!!status);
5485 }
5486
5487 return -ENXIO;
5488}
5489
5490static int light_set_status(int status)
5491{
5492 int rc;
5493
5494 if (tp_features.light) {
5495 if (cmos_handle) {
5496 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005497 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005498 TP_CMOS_THINKLIGHT_ON :
5499 TP_CMOS_THINKLIGHT_OFF);
5500 } else {
5501 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005502 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005503 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005504 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005505 }
5506
5507 return -ENXIO;
5508}
5509
Hans de Goede86ec0c22017-02-09 16:44:12 +01005510static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005511 enum led_brightness brightness)
5512{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005513 return light_set_status((brightness != LED_OFF) ?
5514 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005515}
5516
5517static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5518{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005519 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005520}
5521
5522static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5523 .led_classdev = {
5524 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005525 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005526 .brightness_get = &light_sysfs_get,
5527 }
5528};
5529
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005530static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005532 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005533
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005534 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5535
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005536 if (tpacpi_is_ibm()) {
5537 TPACPI_ACPIHANDLE_INIT(ledb);
5538 TPACPI_ACPIHANDLE_INIT(lght);
5539 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005540 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005541
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005542 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005543 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005544
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005545 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005546 /* light status not supported on
5547 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005548 tp_features.light_status =
5549 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005551 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5552 str_supported(tp_features.light),
5553 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005554
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005555 if (!tp_features.light)
5556 return 1;
5557
5558 rc = led_classdev_register(&tpacpi_pdev->dev,
5559 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005560
5561 if (rc < 0) {
5562 tp_features.light = 0;
5563 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005564 } else {
5565 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005566 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005567
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005568 return rc;
5569}
5570
5571static void light_exit(void)
5572{
5573 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574}
5575
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005576static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005578 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005580 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005581 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005582 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005583 seq_printf(m, "status:\t\tunknown\n");
5584 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005585 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005586 status = light_get_status();
5587 if (status < 0)
5588 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005589 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5590 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594}
5595
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005596static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005599 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005600
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005601 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005602 return -ENODEV;
5603
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005604 while ((cmd = strsep(&buf, ","))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005606 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005608 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 } else
5610 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 }
5612
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005613 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614}
5615
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005616static struct ibm_struct light_driver_data = {
5617 .name = "light",
5618 .read = light_read,
5619 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005620 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005621};
5622
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005623/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005624 * CMOS subdriver
5625 */
5626
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005627/* sysfs cmos_command -------------------------------------------------- */
5628static ssize_t cmos_command_store(struct device *dev,
5629 struct device_attribute *attr,
5630 const char *buf, size_t count)
5631{
5632 unsigned long cmos_cmd;
5633 int res;
5634
5635 if (parse_strtoul(buf, 21, &cmos_cmd))
5636 return -EINVAL;
5637
5638 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005639 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005640}
5641
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005642static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005643
5644/* --------------------------------------------------------------------- */
5645
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005646static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005647{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005648 int res;
5649
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005650 vdbg_printk(TPACPI_DBG_INIT,
5651 "initializing cmos commands subdriver\n");
5652
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005653 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005654
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005655 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5656 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005657
5658 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5659 if (res)
5660 return res;
5661
Jan van den Berg72a979f2014-09-17 00:01:08 +02005662 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005663}
5664
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005665static void cmos_exit(void)
5666{
5667 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5668}
5669
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005670static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005672 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5673 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005675 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005677 seq_printf(m, "status:\t\tsupported\n");
5678 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
5680
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682}
5683
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005684static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005687 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005689 while ((cmd = strsep(&buf, ","))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005690 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5691 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 /* cmos_cmd set */
5693 } else
5694 return -EINVAL;
5695
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005696 res = issue_thinkpad_cmos_command(cmos_cmd);
5697 if (res)
5698 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 }
5700
5701 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005702}
5703
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005704static struct ibm_struct cmos_driver_data = {
5705 .name = "cmos",
5706 .read = cmos_read,
5707 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005708 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005709};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005710
5711/*************************************************************************
5712 * LED subdriver
5713 */
5714
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005715enum led_access_mode {
5716 TPACPI_LED_NONE = 0,
5717 TPACPI_LED_570, /* 570 */
5718 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5719 TPACPI_LED_NEW, /* all others */
5720};
5721
5722enum { /* For TPACPI_LED_OLD */
5723 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5724 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5725 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5726};
5727
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005728static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005729
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005730static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005731
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005732#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005733static struct tpacpi_led_classdev *tpacpi_leds;
5734static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005735static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005736 /* there's a limit of 19 chars + NULL before 2.6.26 */
5737 "tpacpi::power",
5738 "tpacpi:orange:batt",
5739 "tpacpi:green:batt",
5740 "tpacpi::dock_active",
5741 "tpacpi::bay_active",
5742 "tpacpi::dock_batt",
5743 "tpacpi::unknown_led",
5744 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005745 "tpacpi::dock_status1",
5746 "tpacpi::dock_status2",
Hans de Goedebe892e92021-11-23 22:05:24 +01005747 "tpacpi::lid_logo_dot",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005748 "tpacpi::unknown_led3",
5749 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005750};
Hans de Goedebe892e92021-11-23 22:05:24 +01005751#define TPACPI_SAFE_LEDS 0x1481U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005752
5753static inline bool tpacpi_is_led_restricted(const unsigned int led)
5754{
5755#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5756 return false;
5757#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005758 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005759#endif
5760}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005761
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005762static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005763{
5764 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005765 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005766
5767 switch (led_supported) {
5768 case TPACPI_LED_570:
5769 if (!acpi_evalf(ec_handle,
5770 &status, "GLED", "dd", 1 << led))
5771 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005772 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005773 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005774 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005775 TPACPI_LED_ON :
5776 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005777 tpacpi_led_state_cache[led] = led_s;
5778 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005779 default:
5780 return -ENXIO;
5781 }
5782
5783 /* not reached */
5784}
5785
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005786static int led_set_status(const unsigned int led,
5787 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005788{
5789 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005790 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5791 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005792
5793 int rc = 0;
5794
5795 switch (led_supported) {
5796 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005797 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005798 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005799 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005800 if (unlikely(tpacpi_is_led_restricted(led)))
5801 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005802 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5803 (1 << led), led_sled_arg1[ledstatus]))
Pavel Machekaac33112019-04-29 17:21:48 +02005804 return -EIO;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005805 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005806 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005807 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005808 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005809 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005810 if (unlikely(tpacpi_is_led_restricted(led)))
5811 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005812 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5813 if (rc >= 0)
5814 rc = ec_write(TPACPI_LED_EC_HLBL,
5815 (ledstatus == TPACPI_LED_BLINK) << led);
5816 if (rc >= 0)
5817 rc = ec_write(TPACPI_LED_EC_HLCL,
5818 (ledstatus != TPACPI_LED_OFF) << led);
5819 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005820 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005821 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005822 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5823 return -EINVAL;
5824 if (unlikely(tpacpi_is_led_restricted(led)))
5825 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005826 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5827 led, led_led_arg1[ledstatus]))
Pavel Machekaac33112019-04-29 17:21:48 +02005828 return -EIO;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005829 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005830 default:
Pavel Machekaac33112019-04-29 17:21:48 +02005831 return -ENXIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005832 }
5833
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005834 if (!rc)
5835 tpacpi_led_state_cache[led] = ledstatus;
5836
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005837 return rc;
5838}
5839
Hans de Goede86ec0c22017-02-09 16:44:12 +01005840static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005841 enum led_brightness brightness)
5842{
5843 struct tpacpi_led_classdev *data = container_of(led_cdev,
5844 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005845 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005846
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005847 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005848 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005849 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005850 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005851 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005852 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005853
Hans de Goede86ec0c22017-02-09 16:44:12 +01005854 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005855}
5856
5857static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5858 unsigned long *delay_on, unsigned long *delay_off)
5859{
5860 struct tpacpi_led_classdev *data = container_of(led_cdev,
5861 struct tpacpi_led_classdev, led_classdev);
5862
5863 /* Can we choose the flash rate? */
5864 if (*delay_on == 0 && *delay_off == 0) {
5865 /* yes. set them to the hardware blink rate (1 Hz) */
5866 *delay_on = 500; /* ms */
5867 *delay_off = 500; /* ms */
5868 } else if ((*delay_on != 500) || (*delay_off != 500))
5869 return -EINVAL;
5870
Hans de Goede86ec0c22017-02-09 16:44:12 +01005871 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005872}
5873
5874static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5875{
5876 int rc;
5877
5878 struct tpacpi_led_classdev *data = container_of(led_cdev,
5879 struct tpacpi_led_classdev, led_classdev);
5880
5881 rc = led_get_status(data->led);
5882
5883 if (rc == TPACPI_LED_OFF || rc < 0)
5884 rc = LED_OFF; /* no error handling in led class :( */
5885 else
5886 rc = LED_FULL;
5887
5888 return rc;
5889}
5890
5891static void led_exit(void)
5892{
5893 unsigned int i;
5894
Andy Shevchenko41f80042020-05-07 19:37:23 +03005895 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5896 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005897
5898 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005899}
5900
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005901static int __init tpacpi_init_led(unsigned int led)
5902{
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005903 /* LEDs with no name don't get registered */
5904 if (!tpacpi_led_names[led])
5905 return 0;
5906
Hans de Goede86ec0c22017-02-09 16:44:12 +01005907 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005908 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5909 if (led_supported == TPACPI_LED_570)
Andy Shevchenko41f80042020-05-07 19:37:23 +03005910 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005911
5912 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
Andy Shevchenko41f80042020-05-07 19:37:23 +03005913 tpacpi_leds[led].led = led;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005914
Andy Shevchenko41f80042020-05-07 19:37:23 +03005915 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005916}
5917
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005918static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5919 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5920 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5921 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5922
5923 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5924 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5925 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5926 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5927 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5928 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5929 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5930 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5931
5932 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5933 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5934 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5935 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5936 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5937
5938 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5939 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5940 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5941 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5942
5943 /* (1) - may have excess leds enabled on MSB */
5944
5945 /* Defaults (order matters, keep last, don't reorder!) */
5946 { /* Lenovo */
5947 .vendor = PCI_VENDOR_ID_LENOVO,
5948 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5949 .quirks = 0x1fffU,
5950 },
5951 { /* IBM ThinkPads with no EC version string */
5952 .vendor = PCI_VENDOR_ID_IBM,
5953 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5954 .quirks = 0x00ffU,
5955 },
5956 { /* IBM ThinkPads with EC version string */
5957 .vendor = PCI_VENDOR_ID_IBM,
5958 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5959 .quirks = 0x00bfU,
5960 },
5961};
5962
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005963static enum led_access_mode __init led_init_detect_mode(void)
5964{
5965 acpi_status status;
5966
5967 if (tpacpi_is_ibm()) {
5968 /* 570 */
5969 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5970 if (ACPI_SUCCESS(status))
5971 return TPACPI_LED_570;
5972
5973 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5974 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5975 if (ACPI_SUCCESS(status))
5976 return TPACPI_LED_OLD;
5977 }
5978
5979 /* most others */
5980 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5981 if (ACPI_SUCCESS(status))
5982 return TPACPI_LED_NEW;
5983
5984 /* R30, R31, and unknown firmwares */
5985 led_handle = NULL;
5986 return TPACPI_LED_NONE;
5987}
5988
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005989static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005990{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005991 unsigned int i;
5992 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005993 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005994
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005995 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5996
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005997 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005998
Adam Leefcb44e12013-06-07 16:20:08 +08005999 if (led_supported != TPACPI_LED_NONE) {
6000 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6001 ARRAY_SIZE(led_useful_qtable));
6002
6003 if (!useful_leds) {
6004 led_handle = NULL;
6005 led_supported = TPACPI_LED_NONE;
6006 }
6007 }
6008
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006009 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6010 str_supported(led_supported), led_supported);
6011
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006012 if (led_supported == TPACPI_LED_NONE)
6013 return 1;
6014
Kees Cook6396bb22018-06-12 14:03:40 -07006015 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006016 GFP_KERNEL);
6017 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07006018 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006019 return -ENOMEM;
6020 }
6021
6022 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08006023 tpacpi_leds[i].led = -1;
6024
Andy Shevchenko41f80042020-05-07 19:37:23 +03006025 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006026 rc = tpacpi_init_led(i);
6027 if (rc < 0) {
6028 led_exit();
6029 return rc;
6030 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006031 }
6032 }
6033
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006034#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006035 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006036#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006037 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006038}
6039
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006040#define str_led_status(s) \
6041 ((s) == TPACPI_LED_OFF ? "off" : \
6042 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006043
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006044static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006046 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006047 seq_printf(m, "status:\t\tnot supported\n");
6048 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006049 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006050 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006051
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006052 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006053 /* 570 */
6054 int i, status;
6055 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006056 status = led_get_status(i);
6057 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006058 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006059 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006060 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006061 }
6062 }
6063
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006064 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067}
6068
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006069static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070{
6071 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006072 int led, rc;
6073 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006074
6075 if (!led_supported)
6076 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03006078 while ((cmd = strsep(&buf, ","))) {
Adam Leeedf2d772013-06-08 16:51:15 +08006079 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 return -EINVAL;
6081
Andy Shevchenko41f80042020-05-07 19:37:23 +03006082 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
6083 return -ENODEV;
6084
6085 if (tpacpi_leds[led].led < 0)
Adam Leeedf2d772013-06-08 16:51:15 +08006086 return -ENODEV;
6087
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006088 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006089 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006091 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006092 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006093 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006094 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006095 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006096 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006097
6098 rc = led_set_status(led, s);
6099 if (rc < 0)
6100 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 }
6102
6103 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006104}
6105
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006106static struct ibm_struct led_driver_data = {
6107 .name = "led",
6108 .read = led_read,
6109 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006110 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006111};
6112
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006113/*************************************************************************
6114 * Beep subdriver
6115 */
6116
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006117TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006118
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006119#define TPACPI_BEEP_Q1 0x0001
6120
6121static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6122 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6123 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6124};
6125
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006126static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006127{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006128 unsigned long quirks;
6129
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006130 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6131
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006132 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006133
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006134 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6135 str_supported(beep_handle != NULL));
6136
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006137 quirks = tpacpi_check_quirks(beep_quirk_table,
6138 ARRAY_SIZE(beep_quirk_table));
6139
6140 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6141
Jan van den Berg72a979f2014-09-17 00:01:08 +02006142 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006143}
6144
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006145static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006147 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006148 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006149 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006150 seq_printf(m, "status:\t\tsupported\n");
6151 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155}
6156
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006157static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158{
6159 char *cmd;
6160 int beep_cmd;
6161
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006162 if (!beep_handle)
6163 return -ENODEV;
6164
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03006165 while ((cmd = strsep(&buf, ","))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006166 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6167 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 /* beep_cmd set */
6169 } else
6170 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006171 if (tp_features.beep_needs_two_args) {
6172 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6173 beep_cmd, 0))
6174 return -EIO;
6175 } else {
6176 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6177 beep_cmd))
6178 return -EIO;
6179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 }
6181
6182 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006183}
6184
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006185static struct ibm_struct beep_driver_data = {
6186 .name = "beep",
6187 .read = beep_read,
6188 .write = beep_write,
6189};
6190
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006191/*************************************************************************
6192 * Thermal subdriver
6193 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006194
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006195enum thermal_access_mode {
6196 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6197 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6198 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6199 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6200 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6201};
6202
6203enum { /* TPACPI_THERMAL_TPEC_* */
6204 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6205 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
Mark Pearson6759e182021-04-07 17:20:15 -04006206 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006207 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006208
6209 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006210};
6211
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006212
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006213#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6214struct ibm_thermal_sensors_struct {
6215 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6216};
6217
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006218static enum thermal_access_mode thermal_read_mode;
Hans de Goede00cc4262021-04-13 09:21:12 +02006219static const struct attribute_group *thermal_attr_group;
6220static bool thermal_use_labels;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006221
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006222/* idx is zero-based */
6223static int thermal_get_sensor(int idx, s32 *value)
6224{
6225 int t;
6226 s8 tmp;
6227 char tmpi[5];
6228
6229 t = TP_EC_THERMAL_TMP0;
6230
6231 switch (thermal_read_mode) {
6232#if TPACPI_MAX_THERMAL_SENSORS >= 16
6233 case TPACPI_THERMAL_TPEC_16:
6234 if (idx >= 8 && idx <= 15) {
6235 t = TP_EC_THERMAL_TMP8;
6236 idx -= 8;
6237 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006238#endif
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006239 fallthrough;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006240 case TPACPI_THERMAL_TPEC_8:
6241 if (idx <= 7) {
6242 if (!acpi_ec_read(t + idx, &tmp))
6243 return -EIO;
6244 *value = tmp * 1000;
6245 return 0;
6246 }
6247 break;
6248
6249 case TPACPI_THERMAL_ACPI_UPDT:
6250 if (idx <= 7) {
6251 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6252 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6253 return -EIO;
6254 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6255 return -EIO;
6256 *value = (t - 2732) * 100;
6257 return 0;
6258 }
6259 break;
6260
6261 case TPACPI_THERMAL_ACPI_TMP07:
6262 if (idx <= 7) {
6263 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6264 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6265 return -EIO;
6266 if (t > 127 || t < -127)
6267 t = TP_EC_THERMAL_TMP_NA;
6268 *value = t * 1000;
6269 return 0;
6270 }
6271 break;
6272
6273 case TPACPI_THERMAL_NONE:
6274 default:
6275 return -ENOSYS;
6276 }
6277
6278 return -EINVAL;
6279}
6280
6281static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6282{
6283 int res, i;
6284 int n;
6285
6286 n = 8;
6287 i = 0;
6288
6289 if (!s)
6290 return -EINVAL;
6291
6292 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6293 n = 16;
6294
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006295 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006296 res = thermal_get_sensor(i, &s->temp[i]);
6297 if (res)
6298 return res;
6299 }
6300
6301 return n;
6302}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006303
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006304static void thermal_dump_all_sensors(void)
6305{
6306 int n, i;
6307 struct ibm_thermal_sensors_struct t;
6308
6309 n = thermal_get_sensors(&t);
6310 if (n <= 0)
6311 return;
6312
Joe Perches0978e012011-04-04 10:06:25 -07006313 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006314
6315 for (i = 0; i < n; i++) {
6316 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006317 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006318 else
Joe Perches0978e012011-04-04 10:06:25 -07006319 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006320 }
6321
Joe Perches0978e012011-04-04 10:06:25 -07006322 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006323}
6324
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006325/* sysfs temp##_input -------------------------------------------------- */
6326
6327static ssize_t thermal_temp_input_show(struct device *dev,
6328 struct device_attribute *attr,
6329 char *buf)
6330{
6331 struct sensor_device_attribute *sensor_attr =
6332 to_sensor_dev_attr(attr);
6333 int idx = sensor_attr->index;
6334 s32 value;
6335 int res;
6336
6337 res = thermal_get_sensor(idx, &value);
6338 if (res)
6339 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006340 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006341 return -ENXIO;
6342
Ye Guojin33ce79b2021-10-18 09:17:50 +00006343 return sysfs_emit(buf, "%d\n", value);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006344}
6345
6346#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006347 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6348 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006349
6350static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6351 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6352 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6353 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6354 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6355 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6356 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6357 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6358 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6359 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6360 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6361 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6362 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6363 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6364 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6365 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6366 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6367};
6368
6369#define THERMAL_ATTRS(X) \
6370 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6371
6372static struct attribute *thermal_temp_input_attr[] = {
6373 THERMAL_ATTRS(8),
6374 THERMAL_ATTRS(9),
6375 THERMAL_ATTRS(10),
6376 THERMAL_ATTRS(11),
6377 THERMAL_ATTRS(12),
6378 THERMAL_ATTRS(13),
6379 THERMAL_ATTRS(14),
6380 THERMAL_ATTRS(15),
6381 THERMAL_ATTRS(0),
6382 THERMAL_ATTRS(1),
6383 THERMAL_ATTRS(2),
6384 THERMAL_ATTRS(3),
6385 THERMAL_ATTRS(4),
6386 THERMAL_ATTRS(5),
6387 THERMAL_ATTRS(6),
6388 THERMAL_ATTRS(7),
6389 NULL
6390};
6391
6392static const struct attribute_group thermal_temp_input16_group = {
6393 .attrs = thermal_temp_input_attr
6394};
6395
6396static const struct attribute_group thermal_temp_input8_group = {
6397 .attrs = &thermal_temp_input_attr[8]
6398};
6399
6400#undef THERMAL_SENSOR_ATTR_TEMP
6401#undef THERMAL_ATTRS
6402
Hans de Goede00cc4262021-04-13 09:21:12 +02006403static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6404{
6405 return sysfs_emit(buf, "CPU\n");
6406}
6407static DEVICE_ATTR_RO(temp1_label);
6408
6409static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6410{
6411 return sysfs_emit(buf, "GPU\n");
6412}
6413static DEVICE_ATTR_RO(temp2_label);
6414
6415static struct attribute *temp_label_attributes[] = {
6416 &dev_attr_temp1_label.attr,
6417 &dev_attr_temp2_label.attr,
6418 NULL
6419};
6420
6421static const struct attribute_group temp_label_attr_group = {
6422 .attrs = temp_label_attributes,
6423};
6424
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006425/* --------------------------------------------------------------------- */
6426
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006427static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006428{
Mark Pearson6759e182021-04-07 17:20:15 -04006429 u8 t, ta1, ta2, ver = 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006430 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006431 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006432 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006433
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006434 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6435
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006436 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006437
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006438 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006439 /*
6440 * Direct EC access mode: sensors at registers
6441 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6442 * non-implemented, thermal sensors return 0x80 when
6443 * not available
Mark Pearson6759e182021-04-07 17:20:15 -04006444 * The above rule is unfortunately flawed. This has been seen with
6445 * 0xC2 (power supply ID) causing thermal control problems.
6446 * The EC version can be determined by offset 0xEF and at least for
6447 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6448 * are not thermal registers.
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006449 */
Mark Pearson6759e182021-04-07 17:20:15 -04006450 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6451 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006452
6453 ta1 = ta2 = 0;
6454 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006455 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006456 ta1 |= t;
6457 } else {
6458 ta1 = 0;
6459 break;
6460 }
Mark Pearson6759e182021-04-07 17:20:15 -04006461 if (ver < 3) {
6462 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6463 ta2 |= t;
6464 } else {
6465 ta1 = 0;
6466 break;
6467 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006468 }
6469 }
6470 if (ta1 == 0) {
6471 /* This is sheer paranoia, but we handle it anyway */
6472 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006473 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006474 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006475 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006476 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006477 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006478 }
6479 } else {
Hans de Goede00cc4262021-04-13 09:21:12 +02006480 if (ver >= 3) {
Mark Pearson6759e182021-04-07 17:20:15 -04006481 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
Hans de Goede00cc4262021-04-13 09:21:12 +02006482 thermal_use_labels = true;
6483 } else {
Mark Pearson6759e182021-04-07 17:20:15 -04006484 thermal_read_mode =
6485 (ta2 != 0) ?
6486 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Hans de Goede00cc4262021-04-13 09:21:12 +02006487 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006488 }
6489 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006490 if (tpacpi_is_ibm() &&
6491 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006492 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006493 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006494 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006495 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006496 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006497 }
6498 } else {
6499 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006500 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006501 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006502
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006503 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6504 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6505 thermal_read_mode);
6506
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006507 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006508 case TPACPI_THERMAL_TPEC_16:
Hans de Goede00cc4262021-04-13 09:21:12 +02006509 thermal_attr_group = &thermal_temp_input16_group;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006510 break;
6511 case TPACPI_THERMAL_TPEC_8:
6512 case TPACPI_THERMAL_ACPI_TMP07:
6513 case TPACPI_THERMAL_ACPI_UPDT:
Hans de Goede00cc4262021-04-13 09:21:12 +02006514 thermal_attr_group = &thermal_temp_input8_group;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006515 break;
6516 case TPACPI_THERMAL_NONE:
6517 default:
6518 return 1;
6519 }
6520
Hans de Goede00cc4262021-04-13 09:21:12 +02006521 res = sysfs_create_group(&tpacpi_hwmon->kobj, thermal_attr_group);
6522 if (res)
6523 return res;
6524
6525 if (thermal_use_labels) {
6526 res = sysfs_create_group(&tpacpi_hwmon->kobj, &temp_label_attr_group);
6527 if (res) {
6528 sysfs_remove_group(&tpacpi_hwmon->kobj, thermal_attr_group);
6529 return res;
6530 }
6531 }
6532
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006533 return 0;
6534}
6535
6536static void thermal_exit(void)
6537{
Hans de Goede00cc4262021-04-13 09:21:12 +02006538 if (thermal_attr_group)
6539 sysfs_remove_group(&tpacpi_hwmon->kobj, thermal_attr_group);
6540
6541 if (thermal_use_labels)
6542 sysfs_remove_group(&tpacpi_hwmon->kobj, &temp_label_attr_group);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006543}
6544
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006545static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006546{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006547 int n, i;
6548 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006549
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006550 n = thermal_get_sensors(&t);
6551 if (unlikely(n < 0))
6552 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006553
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006554 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006555
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006556 if (n > 0) {
6557 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006558 seq_printf(m, "%d ", t.temp[i] / 1000);
6559 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006560 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006561 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006562
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006563 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006564}
6565
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006566static struct ibm_struct thermal_driver_data = {
6567 .name = "thermal",
6568 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006569 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006570};
6571
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006572/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006573 * Backlight/brightness subdriver
6574 */
Holger Macht8acb0252006-10-20 14:30:28 -07006575
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006576#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6577
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006578/*
6579 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6580 * CMOS NVRAM byte 0x5E, bits 0-3.
6581 *
6582 * EC HBRV (0x31) has the following layout
6583 * Bit 7: unknown function
6584 * Bit 6: unknown function
6585 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6586 * Bit 4: must be set to zero to avoid problems
6587 * Bit 3-0: backlight brightness level
6588 *
6589 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006590 *
6591 * WARNING: The X61 has been verified to use HBRV for something else, so
6592 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6593 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006594 */
6595
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006596enum {
6597 TP_EC_BACKLIGHT = 0x31,
6598
6599 /* TP_EC_BACKLIGHT bitmasks */
6600 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6601 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6602 TP_EC_BACKLIGHT_MAPSW = 0x20,
6603};
6604
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006605enum tpacpi_brightness_access_mode {
6606 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6607 TPACPI_BRGHT_MODE_EC, /* EC control */
6608 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6609 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6610 TPACPI_BRGHT_MODE_MAX
6611};
6612
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006613static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006614
6615static enum tpacpi_brightness_access_mode brightness_mode =
6616 TPACPI_BRGHT_MODE_MAX;
6617
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006618static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6619
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006620static struct mutex brightness_mutex;
6621
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006622/* NVRAM brightness access,
6623 * call with brightness_mutex held! */
6624static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006625{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006626 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006627
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006628 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6629 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6630 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006631 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006632
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006633 return lnvram;
6634}
6635
6636static void tpacpi_brightness_checkpoint_nvram(void)
6637{
6638 u8 lec = 0;
6639 u8 b_nvram;
6640
6641 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6642 return;
6643
6644 vdbg_printk(TPACPI_DBG_BRGHT,
6645 "trying to checkpoint backlight level to NVRAM...\n");
6646
6647 if (mutex_lock_killable(&brightness_mutex) < 0)
6648 return;
6649
6650 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6651 goto unlock;
6652 lec &= TP_EC_BACKLIGHT_LVLMSK;
6653 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6654
6655 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6656 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6657 /* NVRAM needs update */
6658 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6659 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6660 b_nvram |= lec;
6661 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6662 dbg_printk(TPACPI_DBG_BRGHT,
6663 "updated NVRAM backlight level to %u (0x%02x)\n",
6664 (unsigned int) lec, (unsigned int) b_nvram);
6665 } else
6666 vdbg_printk(TPACPI_DBG_BRGHT,
6667 "NVRAM backlight level already is %u (0x%02x)\n",
6668 (unsigned int) lec, (unsigned int) b_nvram);
6669
6670unlock:
6671 mutex_unlock(&brightness_mutex);
6672}
6673
6674
6675/* call with brightness_mutex held! */
6676static int tpacpi_brightness_get_raw(int *status)
6677{
6678 u8 lec = 0;
6679
6680 switch (brightness_mode) {
6681 case TPACPI_BRGHT_MODE_UCMS_STEP:
6682 *status = tpacpi_brightness_nvram_get();
6683 return 0;
6684 case TPACPI_BRGHT_MODE_EC:
6685 case TPACPI_BRGHT_MODE_ECNVRAM:
6686 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006687 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006688 *status = lec;
6689 return 0;
6690 default:
6691 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006692 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006693}
6694
6695/* call with brightness_mutex held! */
6696/* do NOT call with illegal backlight level value */
6697static int tpacpi_brightness_set_ec(unsigned int value)
6698{
6699 u8 lec = 0;
6700
6701 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6702 return -EIO;
6703
6704 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6705 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6706 (value & TP_EC_BACKLIGHT_LVLMSK))))
6707 return -EIO;
6708
6709 return 0;
6710}
6711
6712/* call with brightness_mutex held! */
6713static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6714{
6715 int cmos_cmd, inc;
6716 unsigned int current_value, i;
6717
6718 current_value = tpacpi_brightness_nvram_get();
6719
6720 if (value == current_value)
6721 return 0;
6722
6723 cmos_cmd = (value > current_value) ?
6724 TP_CMOS_BRIGHTNESS_UP :
6725 TP_CMOS_BRIGHTNESS_DOWN;
6726 inc = (value > current_value) ? 1 : -1;
6727
6728 for (i = current_value; i != value; i += inc)
6729 if (issue_thinkpad_cmos_command(cmos_cmd))
6730 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006731
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006732 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006733}
6734
6735/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006736static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006737{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006738 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006739
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006740 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006741 return -EINVAL;
6742
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006743 vdbg_printk(TPACPI_DBG_BRGHT,
6744 "set backlight level to %d\n", value);
6745
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006746 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006747 if (res < 0)
6748 return res;
6749
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006750 switch (brightness_mode) {
6751 case TPACPI_BRGHT_MODE_EC:
6752 case TPACPI_BRGHT_MODE_ECNVRAM:
6753 res = tpacpi_brightness_set_ec(value);
6754 break;
6755 case TPACPI_BRGHT_MODE_UCMS_STEP:
6756 res = tpacpi_brightness_set_ucmsstep(value);
6757 break;
6758 default:
6759 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006760 }
6761
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006762 mutex_unlock(&brightness_mutex);
6763 return res;
6764}
6765
6766/* sysfs backlight class ----------------------------------------------- */
6767
6768static int brightness_update_status(struct backlight_device *bd)
6769{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006770 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006771 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6772 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006773 bd->props.brightness : 0;
6774
6775 dbg_printk(TPACPI_DBG_BRGHT,
6776 "backlight: attempt to set level to %d\n",
6777 level);
6778
6779 /* it is the backlight class's job (caller) to handle
6780 * EINTR and other errors properly */
6781 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006782}
Holger Macht8acb0252006-10-20 14:30:28 -07006783
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006784static int brightness_get(struct backlight_device *bd)
6785{
6786 int status, res;
6787
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006788 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006789 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006790 return 0;
6791
6792 res = tpacpi_brightness_get_raw(&status);
6793
6794 mutex_unlock(&brightness_mutex);
6795
6796 if (res < 0)
6797 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006798
6799 return status & TP_EC_BACKLIGHT_LVLMSK;
6800}
6801
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006802static void tpacpi_brightness_notify_change(void)
6803{
6804 backlight_force_update(ibm_backlight_device,
6805 BACKLIGHT_UPDATE_HOTKEY);
6806}
6807
Lionel Debrouxacc24722010-11-16 14:14:02 +01006808static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006809 .get_brightness = brightness_get,
6810 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006811};
6812
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006813/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006814
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006815/*
6816 * Call _BCL method of video device. On some ThinkPads this will
6817 * switch the firmware to the ACPI brightness control mode.
6818 */
6819
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006820static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6821{
6822 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6823 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006824 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006825 int rc;
6826
Aaron Lu46445b62013-10-11 21:27:46 +08006827 if (acpi_bus_get_device(handle, &device))
6828 return 0;
6829
6830 rc = 0;
6831 list_for_each_entry(child, &device->children, node) {
6832 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6833 NULL, &buffer);
Aaron Ma720ef732020-10-03 01:09:16 +08006834 if (ACPI_FAILURE(status)) {
6835 buffer.length = ACPI_ALLOCATE_BUFFER;
Aaron Lu46445b62013-10-11 21:27:46 +08006836 continue;
Aaron Ma720ef732020-10-03 01:09:16 +08006837 }
Aaron Lu46445b62013-10-11 21:27:46 +08006838
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006839 obj = (union acpi_object *)buffer.pointer;
6840 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006841 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006842 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006843 rc = 0;
6844 } else {
6845 rc = obj->package.count;
6846 }
Aaron Lu46445b62013-10-11 21:27:46 +08006847 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006848 }
6849
6850 kfree(buffer.pointer);
6851 return rc;
6852}
6853
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006854
6855/*
6856 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6857 */
6858static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6859{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006860 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006861 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006862
Aaron Lu46445b62013-10-11 21:27:46 +08006863 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006864 if (video_device)
6865 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006866
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006867 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006868
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006869 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006870}
6871
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006872/*
6873 * These are only useful for models that have only one possibility
6874 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6875 * these quirks.
6876 */
6877#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6878#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6879#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6880
6881static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6882 /* Models with ATI GPUs known to require ECNVRAM mode */
6883 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6884
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006885 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006886 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006887 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006888 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006889 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6890
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006891 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006892 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006893 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6894 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006895
6896 /* Models with Intel GMA900 */
6897 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6898 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6899 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6900};
6901
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006902/*
6903 * Returns < 0 for error, otherwise sets tp_features.bright_*
6904 * and bright_maxlvl.
6905 */
6906static void __init tpacpi_detect_brightness_capabilities(void)
6907{
6908 unsigned int b;
6909
6910 vdbg_printk(TPACPI_DBG_INIT,
6911 "detecting firmware brightness interface capabilities\n");
6912
6913 /* we could run a quirks check here (same table used by
6914 * brightness_init) if needed */
6915
6916 /*
6917 * We always attempt to detect acpi support, so as to switch
6918 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6919 * going to publish a backlight interface
6920 */
6921 b = tpacpi_check_std_acpi_brightness_support();
6922 switch (b) {
6923 case 16:
6924 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006925 break;
6926 case 8:
6927 case 0:
6928 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006929 break;
6930 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006931 tp_features.bright_unkfw = 1;
6932 bright_maxlvl = b - 1;
6933 }
Eric Curtin15c75622016-01-30 16:55:59 +00006934 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006935}
6936
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006937static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006938{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006939 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006940 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006941 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006942
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006943 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6944
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006945 mutex_init(&brightness_mutex);
6946
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006947 quirks = tpacpi_check_quirks(brightness_quirk_table,
6948 ARRAY_SIZE(brightness_quirk_table));
6949
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006950 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006951
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006952 /* if it is unknown, we don't handle it: it wouldn't be safe */
6953 if (tp_features.bright_unkfw)
6954 return 1;
6955
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006956 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006957 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006958 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006959 return 1;
6960 }
6961
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006962 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006963 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006964 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006965 return 1;
6966 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006967 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006968 return 1;
6969 }
Aaron Ma46713ae2020-07-10 09:56:14 +08006970 } else if (!tp_features.bright_acpimode) {
6971 pr_notice("ACPI backlight interface not available\n");
6972 return 1;
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006973 }
6974
Aaron Ma46713ae2020-07-10 09:56:14 +08006975 pr_notice("ACPI native brightness control enabled\n");
6976
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006977 /*
6978 * Check for module parameter bogosity, note that we
6979 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6980 * able to detect "unspecified"
6981 */
6982 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006983 return -EINVAL;
6984
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006985 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6986 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6987 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006988 if (quirks & TPACPI_BRGHT_Q_EC)
6989 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6990 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006991 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6992
6993 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006994 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006995 brightness_mode);
6996 }
6997
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006998 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006999 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03007000 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7001 brightness_mode == TPACPI_BRGHT_MODE_EC))
7002 return -EINVAL;
7003
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007004 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007005 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02007006
Matthew Garretta19a6ee2010-02-17 16:39:44 -05007007 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07007008 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007009 props.max_brightness = bright_maxlvl;
7010 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05007011 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7012 NULL, NULL,
7013 &ibm_backlight_data,
7014 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007015 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03007016 int rc = PTR_ERR(ibm_backlight_device);
7017 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07007018 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03007019 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007020 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007021 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7022 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007023
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03007024 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007025 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7026 brightness_mode);
7027 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7028 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03007029 }
7030
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03007031 /* Added by mistake in early 2007. Probably useless, but it could
7032 * be working around some unknown firmware problem where the value
7033 * read at startup doesn't match the real hardware state... so leave
7034 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02007035 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00007036
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007037 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007038 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007039 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7040 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08007041 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007042 return 0;
7043}
7044
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007045static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007046{
7047 tpacpi_brightness_checkpoint_nvram();
7048}
7049
7050static void brightness_shutdown(void)
7051{
7052 tpacpi_brightness_checkpoint_nvram();
7053}
7054
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007055static void brightness_exit(void)
7056{
7057 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007058 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007059 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007060 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007061 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007062
7063 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007064}
7065
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007066static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007067{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007068 int level;
7069
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007070 level = brightness_get(NULL);
7071 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007072 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007073 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007074 seq_printf(m, "level:\t\t%d\n", level);
7075 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007076 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7077 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007078 }
7079
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007080 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007081}
7082
7083static int brightness_write(char *buf)
7084{
7085 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007086 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007087 char *cmd;
7088
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007089 level = brightness_get(NULL);
7090 if (level < 0)
7091 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007092
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03007093 while ((cmd = strsep(&buf, ","))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007094 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007095 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007096 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007097 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007098 if (level > 0)
7099 level--;
7100 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007101 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007102 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007103 } else
7104 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007105 }
7106
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007107 tpacpi_disclose_usertask("procfs brightness",
7108 "set level to %d\n", level);
7109
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007110 /*
7111 * Now we know what the final level should be, so we try to set it.
7112 * Doing it this way makes the syscall restartable in case of EINTR
7113 */
7114 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007115 if (!rc && ibm_backlight_device)
7116 backlight_force_update(ibm_backlight_device,
7117 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007118 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007119}
7120
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007121static struct ibm_struct brightness_driver_data = {
7122 .name = "brightness",
7123 .read = brightness_read,
7124 .write = brightness_write,
7125 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007126 .suspend = brightness_suspend,
7127 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007128};
7129
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007130/*************************************************************************
7131 * Volume subdriver
7132 */
7133
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007134/*
7135 * IBM ThinkPads have a simple volume controller with MUTE gating.
7136 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7137 *
7138 * Since the *61 series (and probably also the later *60 series), Lenovo
7139 * ThinkPads only implement the MUTE gate.
7140 *
7141 * EC register 0x30
7142 * Bit 6: MUTE (1 mutes sound)
7143 * Bit 3-0: Volume
7144 * Other bits should be zero as far as we know.
7145 *
7146 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7147 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7148 * such as bit 7 which is used to detect repeated presses of MUTE,
7149 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007150 *
7151 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7152 * in response to user input. Unfortunately, this rarely works well.
7153 * The laptop changes the state of its internal MUTE gate and, on some
7154 * models, sends KEY_MUTE, causing any user code that responds to the
7155 * mute button to get confused. The hardware MUTE gate is also
7156 * unnecessary, since user code can handle the mute button without
7157 * kernel or EC help.
7158 *
7159 * To avoid confusing userspace, we simply disable all EC-based mute
7160 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007161 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007162
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007163#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7164
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007165#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7166#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7167#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7168
Takashi Iwaicab66612013-10-24 16:06:32 +02007169#if SNDRV_CARDS <= 32
7170#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7171#else
7172#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7173#endif
7174static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007175static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307176static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007177
7178struct tpacpi_alsa_data {
7179 struct snd_card *card;
7180 struct snd_ctl_elem_id *ctl_mute_id;
7181 struct snd_ctl_elem_id *ctl_vol_id;
7182};
7183
7184static struct snd_card *alsa_card;
7185
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007186enum {
7187 TP_EC_AUDIO = 0x30,
7188
7189 /* TP_EC_AUDIO bits */
7190 TP_EC_AUDIO_MUTESW = 6,
7191
7192 /* TP_EC_AUDIO bitmasks */
7193 TP_EC_AUDIO_LVL_MSK = 0x0F,
7194 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7195
7196 /* Maximum volume */
7197 TP_EC_VOLUME_MAX = 14,
7198};
7199
7200enum tpacpi_volume_access_mode {
7201 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7202 TPACPI_VOL_MODE_EC, /* Pure EC control */
7203 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7204 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7205 TPACPI_VOL_MODE_MAX
7206};
7207
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007208enum tpacpi_volume_capabilities {
7209 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7210 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7211 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7212 TPACPI_VOL_CAP_MAX
7213};
7214
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007215enum tpacpi_mute_btn_mode {
7216 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7217 /* We don't know what mode 1 is. */
7218 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7219 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7220};
7221
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007222static enum tpacpi_volume_access_mode volume_mode =
7223 TPACPI_VOL_MODE_MAX;
7224
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007225static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307226static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007227static bool software_mute_requested = true;
7228static bool software_mute_active;
7229static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007230
7231/*
7232 * Used to syncronize writers to TP_EC_AUDIO and
7233 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7234 */
7235static struct mutex volume_mutex;
7236
7237static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007238{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007239 u8 lec = 0;
7240 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007241 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007242
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007243 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7244 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007245 if (!volume_control_allowed)
7246 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007247 if (software_mute_active)
7248 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007249
7250 vdbg_printk(TPACPI_DBG_MIXER,
7251 "trying to checkpoint mixer state to NVRAM...\n");
7252
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007253 if (tp_features.mixer_no_level_control)
7254 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7255 else
7256 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7257
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007258 if (mutex_lock_killable(&volume_mutex) < 0)
7259 return;
7260
7261 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7262 goto unlock;
7263 lec &= ec_mask;
7264 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7265
7266 if (lec != (b_nvram & ec_mask)) {
7267 /* NVRAM needs update */
7268 b_nvram &= ~ec_mask;
7269 b_nvram |= lec;
7270 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7271 dbg_printk(TPACPI_DBG_MIXER,
7272 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7273 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007274 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007275 vdbg_printk(TPACPI_DBG_MIXER,
7276 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7277 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007278 }
7279
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007280unlock:
7281 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007282}
7283
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007284static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007285{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007286 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007287
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007288 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7289 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007290
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007291 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007292
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007293 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007294
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007295 return 0;
7296}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007297
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007298static int volume_get_status(u8 *status)
7299{
7300 return volume_get_status_ec(status);
7301}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007302
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007303static int volume_set_status_ec(const u8 status)
7304{
7305 if (!acpi_ec_write(TP_EC_AUDIO, status))
7306 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007307
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007308 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7309
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007310 /*
7311 * On X200s, and possibly on others, it can take a while for
7312 * reads to become correct.
7313 */
7314 msleep(1);
7315
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007316 return 0;
7317}
7318
7319static int volume_set_status(const u8 status)
7320{
7321 return volume_set_status_ec(status);
7322}
7323
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007324/* returns < 0 on error, 0 on no change, 1 on change */
7325static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007326{
7327 int rc;
7328 u8 s, n;
7329
7330 if (mutex_lock_killable(&volume_mutex) < 0)
7331 return -EINTR;
7332
7333 rc = volume_get_status_ec(&s);
7334 if (rc)
7335 goto unlock;
7336
7337 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7338 s & ~TP_EC_AUDIO_MUTESW_MSK;
7339
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007340 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007341 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007342 if (!rc)
7343 rc = 1;
7344 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007345
7346unlock:
7347 mutex_unlock(&volume_mutex);
7348 return rc;
7349}
7350
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007351static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007352{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007353 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007354 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007355 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007356}
7357
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007358static int volume_set_mute(const bool mute)
7359{
7360 int rc;
7361
7362 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7363 (mute) ? "" : "un");
7364
7365 rc = __volume_set_mute_ec(mute);
7366 return (rc < 0) ? rc : 0;
7367}
7368
7369/* returns < 0 on error, 0 on no change, 1 on change */
7370static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007371{
7372 int rc;
7373 u8 s, n;
7374
7375 if (vol > TP_EC_VOLUME_MAX)
7376 return -EINVAL;
7377
7378 if (mutex_lock_killable(&volume_mutex) < 0)
7379 return -EINTR;
7380
7381 rc = volume_get_status_ec(&s);
7382 if (rc)
7383 goto unlock;
7384
7385 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7386
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007387 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007388 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007389 if (!rc)
7390 rc = 1;
7391 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007392
7393unlock:
7394 mutex_unlock(&volume_mutex);
7395 return rc;
7396}
7397
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007398static int volume_set_software_mute(bool startup)
7399{
7400 int result;
7401
7402 if (!tpacpi_is_lenovo())
7403 return -ENODEV;
7404
7405 if (startup) {
7406 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7407 "HAUM", "qd"))
7408 return -EIO;
7409
7410 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7411 "Initial HAUM setting was %d\n",
7412 software_mute_orig_mode);
7413 }
7414
7415 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7416 (int)TP_EC_MUTE_BTN_NONE))
7417 return -EIO;
7418
7419 if (result != TP_EC_MUTE_BTN_NONE)
7420 pr_warn("Unexpected SAUM result %d\n",
7421 result);
7422
7423 /*
7424 * In software mute mode, the standard codec controls take
7425 * precendence, so we unmute the ThinkPad HW switch at
7426 * startup. Just on case there are SAUM-capable ThinkPads
7427 * with level controls, set max HW volume as well.
7428 */
7429 if (tp_features.mixer_no_level_control)
7430 result = volume_set_mute(false);
7431 else
7432 result = volume_set_status(TP_EC_VOLUME_MAX);
7433
7434 if (result != 0)
7435 pr_warn("Failed to unmute the HW mute switch\n");
7436
7437 return 0;
7438}
7439
7440static void volume_exit_software_mute(void)
7441{
7442 int r;
7443
7444 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7445 || r != software_mute_orig_mode)
7446 pr_warn("Failed to restore mute mode\n");
7447}
7448
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007449static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007450{
7451 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007452 "ALSA: trying to set volume level to %hu\n", vol);
7453 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007454}
7455
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007456static void volume_alsa_notify_change(void)
7457{
7458 struct tpacpi_alsa_data *d;
7459
7460 if (alsa_card && alsa_card->private_data) {
7461 d = alsa_card->private_data;
7462 if (d->ctl_mute_id)
7463 snd_ctl_notify(alsa_card,
7464 SNDRV_CTL_EVENT_MASK_VALUE,
7465 d->ctl_mute_id);
7466 if (d->ctl_vol_id)
7467 snd_ctl_notify(alsa_card,
7468 SNDRV_CTL_EVENT_MASK_VALUE,
7469 d->ctl_vol_id);
7470 }
7471}
7472
7473static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7474 struct snd_ctl_elem_info *uinfo)
7475{
7476 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7477 uinfo->count = 1;
7478 uinfo->value.integer.min = 0;
7479 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7480 return 0;
7481}
7482
7483static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7484 struct snd_ctl_elem_value *ucontrol)
7485{
7486 u8 s;
7487 int rc;
7488
7489 rc = volume_get_status(&s);
7490 if (rc < 0)
7491 return rc;
7492
7493 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7494 return 0;
7495}
7496
7497static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7498 struct snd_ctl_elem_value *ucontrol)
7499{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007500 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7501 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007502 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007503}
7504
7505#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7506
7507static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7508 struct snd_ctl_elem_value *ucontrol)
7509{
7510 u8 s;
7511 int rc;
7512
7513 rc = volume_get_status(&s);
7514 if (rc < 0)
7515 return rc;
7516
7517 ucontrol->value.integer.value[0] =
7518 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7519 return 0;
7520}
7521
7522static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7523 struct snd_ctl_elem_value *ucontrol)
7524{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007525 tpacpi_disclose_usertask("ALSA", "%smute\n",
7526 ucontrol->value.integer.value[0] ?
7527 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007528 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007529}
7530
Mathias Krausec3aa4722014-07-16 19:43:17 +02007531static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007532 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7533 .name = "Console Playback Volume",
7534 .index = 0,
7535 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7536 .info = volume_alsa_vol_info,
7537 .get = volume_alsa_vol_get,
7538};
7539
Mathias Krausec3aa4722014-07-16 19:43:17 +02007540static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007541 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7542 .name = "Console Playback Switch",
7543 .index = 0,
7544 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7545 .info = volume_alsa_mute_info,
7546 .get = volume_alsa_mute_get,
7547};
7548
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007549static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007550{
7551 tpacpi_volume_checkpoint_nvram();
7552}
7553
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007554static void volume_resume(void)
7555{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007556 if (software_mute_active) {
7557 if (volume_set_software_mute(false) < 0)
7558 pr_warn("Failed to restore software mute\n");
7559 } else {
7560 volume_alsa_notify_change();
7561 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007562}
7563
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007564static void volume_shutdown(void)
7565{
7566 tpacpi_volume_checkpoint_nvram();
7567}
7568
7569static void volume_exit(void)
7570{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007571 if (alsa_card) {
7572 snd_card_free(alsa_card);
7573 alsa_card = NULL;
7574 }
7575
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007576 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007577
7578 if (software_mute_active)
7579 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007580}
7581
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007582static int __init volume_create_alsa_mixer(void)
7583{
7584 struct snd_card *card;
7585 struct tpacpi_alsa_data *data;
7586 struct snd_kcontrol *ctl_vol;
7587 struct snd_kcontrol *ctl_mute;
7588 int rc;
7589
Takashi Iwai89235e52014-01-29 15:01:27 +01007590 rc = snd_card_new(&tpacpi_pdev->dev,
7591 alsa_index, alsa_id, THIS_MODULE,
7592 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007593 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007594 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007595 return 1;
7596 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007597
7598 BUG_ON(!card->private_data);
7599 data = card->private_data;
7600 data->card = card;
7601
7602 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7603 sizeof(card->driver));
7604 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7605 sizeof(card->shortname));
7606 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7607 (thinkpad_id.ec_version_str) ?
7608 thinkpad_id.ec_version_str : "(unknown)");
7609 snprintf(card->longname, sizeof(card->longname),
7610 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7611 (thinkpad_id.ec_version_str) ?
7612 thinkpad_id.ec_version_str : "unknown");
7613
7614 if (volume_control_allowed) {
7615 volume_alsa_control_vol.put = volume_alsa_vol_put;
7616 volume_alsa_control_vol.access =
7617 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7618
7619 volume_alsa_control_mute.put = volume_alsa_mute_put;
7620 volume_alsa_control_mute.access =
7621 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7622 }
7623
7624 if (!tp_features.mixer_no_level_control) {
7625 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7626 rc = snd_ctl_add(card, ctl_vol);
7627 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007628 pr_err("Failed to create ALSA volume control: %d\n",
7629 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007630 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007631 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007632 data->ctl_vol_id = &ctl_vol->id;
7633 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007634
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007635 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7636 rc = snd_ctl_add(card, ctl_mute);
7637 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007638 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007639 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007640 }
7641 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007642
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007643 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007644 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007645 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007646 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007647 }
7648
7649 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007650 return 0;
7651
7652err_exit:
7653 snd_card_free(card);
7654 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007655}
7656
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007657#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7658#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7659
7660static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7661 /* Whitelist volume level on all IBM by default */
7662 { .vendor = PCI_VENDOR_ID_IBM,
7663 .bios = TPACPI_MATCH_ANY,
7664 .ec = TPACPI_MATCH_ANY,
7665 .quirks = TPACPI_VOL_Q_LEVEL },
7666
7667 /* Lenovo models with volume control (needs confirmation) */
7668 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7669 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7670 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7671 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7672 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7673 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7674 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7675
7676 /* Whitelist mute-only on all Lenovo by default */
7677 { .vendor = PCI_VENDOR_ID_LENOVO,
7678 .bios = TPACPI_MATCH_ANY,
7679 .ec = TPACPI_MATCH_ANY,
7680 .quirks = TPACPI_VOL_Q_MUTEONLY }
7681};
7682
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007683static int __init volume_init(struct ibm_init_struct *iibm)
7684{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007685 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007686 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007687
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007688 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7689
7690 mutex_init(&volume_mutex);
7691
7692 /*
7693 * Check for module parameter bogosity, note that we
7694 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7695 * able to detect "unspecified"
7696 */
7697 if (volume_mode > TPACPI_VOL_MODE_MAX)
7698 return -EINVAL;
7699
7700 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007701 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7702 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007703 return 1;
7704 }
7705
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007706 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7707 return -EINVAL;
7708
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007709 /*
7710 * The ALSA mixer is our primary interface.
7711 * When disabled, don't install the subdriver at all
7712 */
7713 if (!alsa_enable) {
7714 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007715 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007716 return 1;
7717 }
7718
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007719 quirks = tpacpi_check_quirks(volume_quirk_table,
7720 ARRAY_SIZE(volume_quirk_table));
7721
7722 switch (volume_capabilities) {
7723 case TPACPI_VOL_CAP_AUTO:
7724 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7725 tp_features.mixer_no_level_control = 1;
7726 else if (quirks & TPACPI_VOL_Q_LEVEL)
7727 tp_features.mixer_no_level_control = 0;
7728 else
7729 return 1; /* no mixer */
7730 break;
7731 case TPACPI_VOL_CAP_VOLMUTE:
7732 tp_features.mixer_no_level_control = 0;
7733 break;
7734 case TPACPI_VOL_CAP_MUTEONLY:
7735 tp_features.mixer_no_level_control = 1;
7736 break;
7737 default:
7738 return 1;
7739 }
7740
7741 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7742 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7743 "using user-supplied volume_capabilities=%d\n",
7744 volume_capabilities);
7745
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007746 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7747 volume_mode == TPACPI_VOL_MODE_MAX) {
7748 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7749
7750 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7751 "driver auto-selected volume_mode=%d\n",
7752 volume_mode);
7753 } else {
7754 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7755 "using user-supplied volume_mode=%d\n",
7756 volume_mode);
7757 }
7758
7759 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007760 "mute is supported, volume control is %s\n",
7761 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007762
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007763 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7764 software_mute_active = true;
7765 } else {
7766 rc = volume_create_alsa_mixer();
7767 if (rc) {
7768 pr_err("Could not create the ALSA mixer interface\n");
7769 return rc;
7770 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007771
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007772 pr_info("Console audio control enabled, mode: %s\n",
7773 (volume_control_allowed) ?
7774 "override (read/write)" :
7775 "monitor (read only)");
7776 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007777
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007778 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7779 "registering volume hotkeys as change notification\n");
7780 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7781 | TP_ACPI_HKEY_VOLUP_MASK
7782 | TP_ACPI_HKEY_VOLDWN_MASK
7783 | TP_ACPI_HKEY_MUTE_MASK);
7784
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007785 return 0;
7786}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007787
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007788static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007789{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007790 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007791
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007792 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007793 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007794 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007795 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007796 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007797 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007798 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007799 status & TP_EC_AUDIO_LVL_MSK);
7800
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007801 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007802 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007803
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007804 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007805 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007806 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007807 seq_printf(m, "commands:\tup, down\n");
7808 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7809 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007810 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007811 }
7812 }
7813
7814 return 0;
7815}
7816
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007817static int volume_write(char *buf)
7818{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007819 u8 s;
7820 u8 new_level, new_mute;
7821 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007822 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007823 int rc;
7824
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007825 /*
7826 * We do allow volume control at driver startup, so that the
7827 * user can set initial state through the volume=... parameter hack.
7828 */
7829 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7830 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7831 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007832 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7833 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007834 }
7835 return -EPERM;
7836 }
7837
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007838 rc = volume_get_status(&s);
7839 if (rc < 0)
7840 return rc;
7841
7842 new_level = s & TP_EC_AUDIO_LVL_MSK;
7843 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007844
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03007845 while ((cmd = strsep(&buf, ","))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007846 if (!tp_features.mixer_no_level_control) {
7847 if (strlencmp(cmd, "up") == 0) {
7848 if (new_mute)
7849 new_mute = 0;
7850 else if (new_level < TP_EC_VOLUME_MAX)
7851 new_level++;
7852 continue;
7853 } else if (strlencmp(cmd, "down") == 0) {
7854 if (new_mute)
7855 new_mute = 0;
7856 else if (new_level > 0)
7857 new_level--;
7858 continue;
7859 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7860 l >= 0 && l <= TP_EC_VOLUME_MAX) {
Jiapeng Chong3fca4b12021-05-24 14:23:58 +08007861 new_level = l;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007862 continue;
7863 }
7864 }
7865 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007866 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007867 else if (strlencmp(cmd, "unmute") == 0)
7868 new_mute = 0;
7869 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007870 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007871 }
7872
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007873 if (tp_features.mixer_no_level_control) {
7874 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7875 new_mute ? "" : "un");
7876 rc = volume_set_mute(!!new_mute);
7877 } else {
7878 tpacpi_disclose_usertask("procfs volume",
7879 "%smute and set level to %d\n",
7880 new_mute ? "" : "un", new_level);
7881 rc = volume_set_status(new_mute | new_level);
7882 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007883 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007884
7885 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007886}
7887
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007888static struct ibm_struct volume_driver_data = {
7889 .name = "volume",
7890 .read = volume_read,
7891 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007892 .exit = volume_exit,
7893 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007894 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007895 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007896};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007897
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007898#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7899
7900#define alsa_card NULL
7901
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007902static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007903{
7904}
7905
7906static int __init volume_init(struct ibm_init_struct *iibm)
7907{
Joe Perches0978e012011-04-04 10:06:25 -07007908 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007909
7910 return 1;
7911}
7912
7913static struct ibm_struct volume_driver_data = {
7914 .name = "volume",
7915};
7916
7917#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7918
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007919/*************************************************************************
7920 * Fan subdriver
7921 */
7922
7923/*
7924 * FAN ACCESS MODES
7925 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007926 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007927 * ACPI GFAN method: returns fan level
7928 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007929 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007930 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007931 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007932 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007933 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7934 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007935 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7936 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007937 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007938 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007939 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7940 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007941 *
7942 * Fan speed changes of any sort (including those caused by the
7943 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007944 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007945 * limited.
7946 *
7947 * Reading is not available if GFAN exists.
7948 * Writing is not available if SFAN exists.
7949 *
7950 * Bits
7951 * 7 automatic mode engaged;
7952 * (default operation mode of the ThinkPad)
7953 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007954 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007955 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007956 * the tachometer while the fan controller ramps up
7957 * the speed (which can take up to a few *minutes*).
7958 * Speeds up fan to 100% duty-cycle, which is far above
7959 * the standard RPM levels. It is not impossible that
7960 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007961 * 5-3 unused in some models. Extra bits for fan level
7962 * in others, but still useless as all values above
7963 * 7 map to the same speed as level 7 in these models.
7964 * 2-0 fan level (0..7 usually)
7965 * 0x00 = stop
7966 * 0x07 = max (set when temperatures critical)
7967 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007968 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007969 *
7970 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007971 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007972 * does so, its initial value is meaningless (0x07).
7973 *
7974 * For firmware bugs, refer to:
Alexander A. Klimov19206a12020-07-13 21:06:28 +02007975 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007976 *
7977 * ----
7978 *
7979 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7980 * Main fan tachometer reading (in RPM)
7981 *
7982 * This register is present on all ThinkPads with a new-style EC, and
7983 * it is known not to be present on the A21m/e, and T22, as there is
7984 * something else in offset 0x84 according to the ACPI DSDT. Other
7985 * ThinkPads from this same time period (and earlier) probably lack the
7986 * tachometer as well.
7987 *
Nick Andrew877d0312009-01-26 11:06:57 +01007988 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007989 * was never fixed by IBM to report the EC firmware version string
7990 * probably support the tachometer (like the early X models), so
7991 * detecting it is quite hard. We need more data to know for sure.
7992 *
7993 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7994 * might result.
7995 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007996 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7997 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007998 *
7999 * For firmware bugs, refer to:
Alexander A. Klimov19206a12020-07-13 21:06:28 +02008000 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008001 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008002 * ----
8003 *
8004 * ThinkPad EC register 0x31 bit 0 (only on select models)
8005 *
8006 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8007 * show the speed of the main fan. When bit 0 of EC register 0x31
8008 * is one, the tachometer registers show the speed of the auxiliary
8009 * fan.
8010 *
8011 * Fan control seems to affect both fans, regardless of the state
8012 * of this bit.
8013 *
8014 * So far, only the firmware for the X60/X61 non-tablet versions
8015 * seem to support this (firmware TP-7M).
8016 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008017 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008018 * ThinkPad X31, X40, X41. Not available in the X60.
8019 *
8020 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8021 * high speed. ACPI DSDT seems to map these three speeds to levels
8022 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8023 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8024 *
8025 * The speeds are stored on handles
8026 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8027 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008028 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008029 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8030 *
8031 * ACPI DSDT switches which set is in use depending on various
8032 * factors.
8033 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008034 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008035 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8036 * but the ACPI tables just mention level 7.
8037 */
8038
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008039enum { /* Fan control constants */
8040 fan_status_offset = 0x2f, /* EC register 0x2f */
8041 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8042 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008043 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8044 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008045
8046 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8047 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8048
8049 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8050};
8051
8052enum fan_status_access_mode {
8053 TPACPI_FAN_NONE = 0, /* No fan status or control */
8054 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8055 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8056};
8057
8058enum fan_control_access_mode {
8059 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8060 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8061 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8062 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8063};
8064
8065enum fan_control_commands {
8066 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8067 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8068 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8069 * and also watchdog cmd */
8070};
8071
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308072static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008073
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008074static enum fan_status_access_mode fan_status_access_mode;
8075static enum fan_control_access_mode fan_control_access_mode;
8076static enum fan_control_commands fan_control_commands;
8077
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008078static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008079static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008080static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008081static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008082
8083static struct mutex fan_mutex;
8084
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008085static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05008086static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008087
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008088TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8089TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008090 "\\FSPD", /* 600e/x, 770e, 770x */
8091 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008092TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008093 "JFNS", /* 770x-JL */
8094 ); /* all others */
8095
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008096/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008097 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8098 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8099 * be in auto mode (0x80).
8100 *
8101 * This is corrected by any write to HFSP either by the driver, or
8102 * by the firmware.
8103 *
8104 * We assume 0x07 really means auto mode while this quirk is active,
8105 * as this is far more likely than the ThinkPad being in level 7,
8106 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008107 *
8108 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8109 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008110 */
8111
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008112static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008113{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008114 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008115 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008116 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008117 }
8118}
8119
8120static void fan_quirk1_handle(u8 *fan_status)
8121{
8122 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8123 if (*fan_status != fan_control_initial_status) {
8124 /* something changed the HFSP regisnter since
8125 * driver init time, so it is not undefined
8126 * anymore */
8127 tp_features.fan_ctrl_status_undef = 0;
8128 } else {
8129 /* Return most likely status. In fact, it
8130 * might be the only possible status */
8131 *fan_status = TP_EC_FAN_AUTO;
8132 }
8133 }
8134}
8135
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008136/* Select main fan on X60/X61, NOOP on others */
8137static bool fan_select_fan1(void)
8138{
8139 if (tp_features.second_fan) {
8140 u8 val;
8141
8142 if (ec_read(fan_select_offset, &val) < 0)
8143 return false;
8144 val &= 0xFEU;
8145 if (ec_write(fan_select_offset, val) < 0)
8146 return false;
8147 }
8148 return true;
8149}
8150
8151/* Select secondary fan on X60/X61 */
8152static bool fan_select_fan2(void)
8153{
8154 u8 val;
8155
8156 if (!tp_features.second_fan)
8157 return false;
8158
8159 if (ec_read(fan_select_offset, &val) < 0)
8160 return false;
8161 val |= 0x01U;
8162 if (ec_write(fan_select_offset, val) < 0)
8163 return false;
8164
8165 return true;
8166}
8167
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008168/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008169 * Call with fan_mutex held
8170 */
8171static void fan_update_desired_level(u8 status)
8172{
8173 if ((status &
8174 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8175 if (status > 7)
8176 fan_control_desired_level = 7;
8177 else
8178 fan_control_desired_level = status;
8179 }
8180}
8181
8182static int fan_get_status(u8 *status)
8183{
8184 u8 s;
8185
8186 /* TODO:
8187 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8188
8189 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008190 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008191 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008192 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008193
Dan Carpentereceeb432012-09-01 12:54:07 -07008194 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008195 return -EIO;
8196
8197 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008198 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008199
8200 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008201 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008202 case TPACPI_FAN_RD_TPEC:
8203 /* all except 570, 600e/x, 770e, 770x */
8204 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8205 return -EIO;
8206
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008207 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008208 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008209 fan_quirk1_handle(status);
8210 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008211
8212 break;
8213
8214 default:
8215 return -ENXIO;
8216 }
8217
8218 return 0;
8219}
8220
8221static int fan_get_status_safe(u8 *status)
8222{
8223 int rc;
8224 u8 s;
8225
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008226 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008227 return -ERESTARTSYS;
8228 rc = fan_get_status(&s);
8229 if (!rc)
8230 fan_update_desired_level(s);
8231 mutex_unlock(&fan_mutex);
8232
Dan Carpentera7718362016-04-15 17:46:34 +03008233 if (rc)
8234 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008235 if (status)
8236 *status = s;
8237
Dan Carpentera7718362016-04-15 17:46:34 +03008238 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008239}
8240
8241static int fan_get_speed(unsigned int *speed)
8242{
8243 u8 hi, lo;
8244
8245 switch (fan_status_access_mode) {
8246 case TPACPI_FAN_RD_TPEC:
8247 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008248 if (unlikely(!fan_select_fan1()))
8249 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008250 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8251 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8252 return -EIO;
8253
8254 if (likely(speed))
8255 *speed = (hi << 8) | lo;
8256
8257 break;
8258
8259 default:
8260 return -ENXIO;
8261 }
8262
8263 return 0;
8264}
8265
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008266static int fan2_get_speed(unsigned int *speed)
8267{
8268 u8 hi, lo;
8269 bool rc;
8270
8271 switch (fan_status_access_mode) {
8272 case TPACPI_FAN_RD_TPEC:
8273 /* all except 570, 600e/x, 770e, 770x */
8274 if (unlikely(!fan_select_fan2()))
8275 return -EIO;
8276 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8277 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8278 fan_select_fan1(); /* play it safe */
8279 if (rc)
8280 return -EIO;
8281
8282 if (likely(speed))
8283 *speed = (hi << 8) | lo;
8284
8285 break;
8286
8287 default:
8288 return -ENXIO;
8289 }
8290
8291 return 0;
8292}
8293
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008294static int fan_set_level(int level)
8295{
8296 if (!fan_control_allowed)
8297 return -EPERM;
8298
8299 switch (fan_control_access_mode) {
8300 case TPACPI_FAN_WR_ACPI_SFAN:
Lars Hofhansl14232c62020-04-23 14:57:09 -07008301 if ((level < 0) || (level > 7))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008302 return -EINVAL;
Lars Hofhansl14232c62020-04-23 14:57:09 -07008303
8304 if (tp_features.second_fan_ctl) {
8305 if (!fan_select_fan2() ||
8306 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8307 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8308 tp_features.second_fan_ctl = 0;
8309 }
8310 fan_select_fan1();
8311 }
8312 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8313 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008314 break;
8315
8316 case TPACPI_FAN_WR_ACPI_FANS:
8317 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008318 if (!(level & TP_EC_FAN_AUTO) &&
8319 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008320 ((level < 0) || (level > 7)))
8321 return -EINVAL;
8322
8323 /* safety net should the EC not support AUTO
8324 * or FULLSPEED mode bits and just ignore them */
8325 if (level & TP_EC_FAN_FULLSPEED)
8326 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008327 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008328 level |= 4; /* safety min speed 4 */
8329
Lars Hofhansl14232c62020-04-23 14:57:09 -07008330 if (tp_features.second_fan_ctl) {
8331 if (!fan_select_fan2() ||
8332 !acpi_ec_write(fan_status_offset, level)) {
8333 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8334 tp_features.second_fan_ctl = 0;
8335 }
8336 fan_select_fan1();
8337
8338 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008339 if (!acpi_ec_write(fan_status_offset, level))
8340 return -EIO;
8341 else
8342 tp_features.fan_ctrl_status_undef = 0;
8343 break;
8344
8345 default:
8346 return -ENXIO;
8347 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008348
8349 vdbg_printk(TPACPI_DBG_FAN,
8350 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008351 return 0;
8352}
8353
8354static int fan_set_level_safe(int level)
8355{
8356 int rc;
8357
8358 if (!fan_control_allowed)
8359 return -EPERM;
8360
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008361 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008362 return -ERESTARTSYS;
8363
8364 if (level == TPACPI_FAN_LAST_LEVEL)
8365 level = fan_control_desired_level;
8366
8367 rc = fan_set_level(level);
8368 if (!rc)
8369 fan_update_desired_level(level);
8370
8371 mutex_unlock(&fan_mutex);
8372 return rc;
8373}
8374
8375static int fan_set_enable(void)
8376{
8377 u8 s;
8378 int rc;
8379
8380 if (!fan_control_allowed)
8381 return -EPERM;
8382
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008383 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008384 return -ERESTARTSYS;
8385
8386 switch (fan_control_access_mode) {
8387 case TPACPI_FAN_WR_ACPI_FANS:
8388 case TPACPI_FAN_WR_TPEC:
8389 rc = fan_get_status(&s);
8390 if (rc < 0)
8391 break;
8392
8393 /* Don't go out of emergency fan mode */
8394 if (s != 7) {
8395 s &= 0x07;
8396 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8397 }
8398
8399 if (!acpi_ec_write(fan_status_offset, s))
8400 rc = -EIO;
8401 else {
8402 tp_features.fan_ctrl_status_undef = 0;
8403 rc = 0;
8404 }
8405 break;
8406
8407 case TPACPI_FAN_WR_ACPI_SFAN:
8408 rc = fan_get_status(&s);
8409 if (rc < 0)
8410 break;
8411
8412 s &= 0x07;
8413
8414 /* Set fan to at least level 4 */
8415 s |= 4;
8416
8417 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008418 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008419 else
8420 rc = 0;
8421 break;
8422
8423 default:
8424 rc = -ENXIO;
8425 }
8426
8427 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008428
8429 if (!rc)
8430 vdbg_printk(TPACPI_DBG_FAN,
8431 "fan control: set fan control register to 0x%02x\n",
8432 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008433 return rc;
8434}
8435
8436static int fan_set_disable(void)
8437{
8438 int rc;
8439
8440 if (!fan_control_allowed)
8441 return -EPERM;
8442
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008443 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008444 return -ERESTARTSYS;
8445
8446 rc = 0;
8447 switch (fan_control_access_mode) {
8448 case TPACPI_FAN_WR_ACPI_FANS:
8449 case TPACPI_FAN_WR_TPEC:
8450 if (!acpi_ec_write(fan_status_offset, 0x00))
8451 rc = -EIO;
8452 else {
8453 fan_control_desired_level = 0;
8454 tp_features.fan_ctrl_status_undef = 0;
8455 }
8456 break;
8457
8458 case TPACPI_FAN_WR_ACPI_SFAN:
8459 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8460 rc = -EIO;
8461 else
8462 fan_control_desired_level = 0;
8463 break;
8464
8465 default:
8466 rc = -ENXIO;
8467 }
8468
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008469 if (!rc)
8470 vdbg_printk(TPACPI_DBG_FAN,
8471 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008472
8473 mutex_unlock(&fan_mutex);
8474 return rc;
8475}
8476
8477static int fan_set_speed(int speed)
8478{
8479 int rc;
8480
8481 if (!fan_control_allowed)
8482 return -EPERM;
8483
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008484 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008485 return -ERESTARTSYS;
8486
8487 rc = 0;
8488 switch (fan_control_access_mode) {
8489 case TPACPI_FAN_WR_ACPI_FANS:
8490 if (speed >= 0 && speed <= 65535) {
8491 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8492 speed, speed, speed))
8493 rc = -EIO;
8494 } else
8495 rc = -EINVAL;
8496 break;
8497
8498 default:
8499 rc = -ENXIO;
8500 }
8501
8502 mutex_unlock(&fan_mutex);
8503 return rc;
8504}
8505
8506static void fan_watchdog_reset(void)
8507{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008508 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8509 return;
8510
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008511 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008512 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8513 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8514 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8515 else
8516 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008517}
8518
8519static void fan_watchdog_fire(struct work_struct *ignored)
8520{
8521 int rc;
8522
8523 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8524 return;
8525
Joe Perches0978e012011-04-04 10:06:25 -07008526 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008527 rc = fan_set_enable();
8528 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008529 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8530 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008531 /* reschedule for later */
8532 fan_watchdog_reset();
8533 }
8534}
8535
8536/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008537 * SYSFS fan layout: hwmon compatible (device)
8538 *
8539 * pwm*_enable:
8540 * 0: "disengaged" mode
8541 * 1: manual mode
8542 * 2: native EC "auto" mode (recommended, hardware default)
8543 *
8544 * pwm*: set speed in manual mode, ignored otherwise.
8545 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8546 * interpolation.
8547 *
8548 * fan*_input: tachometer reading, RPM
8549 *
8550 *
8551 * SYSFS fan layout: extensions
8552 *
8553 * fan_watchdog (driver):
8554 * fan watchdog interval in seconds, 0 disables (default), max 120
8555 */
8556
8557/* sysfs fan pwm1_enable ----------------------------------------------- */
8558static ssize_t fan_pwm1_enable_show(struct device *dev,
8559 struct device_attribute *attr,
8560 char *buf)
8561{
8562 int res, mode;
8563 u8 status;
8564
8565 res = fan_get_status_safe(&status);
8566 if (res)
8567 return res;
8568
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008569 if (status & TP_EC_FAN_FULLSPEED) {
8570 mode = 0;
8571 } else if (status & TP_EC_FAN_AUTO) {
8572 mode = 2;
8573 } else
8574 mode = 1;
8575
Ye Guojin33ce79b2021-10-18 09:17:50 +00008576 return sysfs_emit(buf, "%d\n", mode);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008577}
8578
8579static ssize_t fan_pwm1_enable_store(struct device *dev,
8580 struct device_attribute *attr,
8581 const char *buf, size_t count)
8582{
8583 unsigned long t;
8584 int res, level;
8585
8586 if (parse_strtoul(buf, 2, &t))
8587 return -EINVAL;
8588
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008589 tpacpi_disclose_usertask("hwmon pwm1_enable",
8590 "set fan mode to %lu\n", t);
8591
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008592 switch (t) {
8593 case 0:
8594 level = TP_EC_FAN_FULLSPEED;
8595 break;
8596 case 1:
8597 level = TPACPI_FAN_LAST_LEVEL;
8598 break;
8599 case 2:
8600 level = TP_EC_FAN_AUTO;
8601 break;
8602 case 3:
8603 /* reserved for software-controlled auto mode */
8604 return -ENOSYS;
8605 default:
8606 return -EINVAL;
8607 }
8608
8609 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008610 if (res == -ENXIO)
8611 return -EINVAL;
8612 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008613 return res;
8614
8615 fan_watchdog_reset();
8616
8617 return count;
8618}
8619
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008620static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8621 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008622
8623/* sysfs fan pwm1 ------------------------------------------------------ */
8624static ssize_t fan_pwm1_show(struct device *dev,
8625 struct device_attribute *attr,
8626 char *buf)
8627{
8628 int res;
8629 u8 status;
8630
8631 res = fan_get_status_safe(&status);
8632 if (res)
8633 return res;
8634
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008635 if ((status &
8636 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8637 status = fan_control_desired_level;
8638
8639 if (status > 7)
8640 status = 7;
8641
Ye Guojin33ce79b2021-10-18 09:17:50 +00008642 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008643}
8644
8645static ssize_t fan_pwm1_store(struct device *dev,
8646 struct device_attribute *attr,
8647 const char *buf, size_t count)
8648{
8649 unsigned long s;
8650 int rc;
8651 u8 status, newlevel;
8652
8653 if (parse_strtoul(buf, 255, &s))
8654 return -EINVAL;
8655
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008656 tpacpi_disclose_usertask("hwmon pwm1",
8657 "set fan speed to %lu\n", s);
8658
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008659 /* scale down from 0-255 to 0-7 */
8660 newlevel = (s >> 5) & 0x07;
8661
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008662 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008663 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008664
8665 rc = fan_get_status(&status);
8666 if (!rc && (status &
8667 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8668 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008669 if (rc == -ENXIO)
8670 rc = -EINVAL;
8671 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008672 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008673 fan_watchdog_reset();
8674 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008675 }
8676
8677 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008678 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008679}
8680
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008681static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008682
8683/* sysfs fan fan1_input ------------------------------------------------ */
8684static ssize_t fan_fan1_input_show(struct device *dev,
8685 struct device_attribute *attr,
8686 char *buf)
8687{
8688 int res;
8689 unsigned int speed;
8690
8691 res = fan_get_speed(&speed);
8692 if (res < 0)
8693 return res;
8694
Ye Guojin33ce79b2021-10-18 09:17:50 +00008695 return sysfs_emit(buf, "%u\n", speed);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008696}
8697
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008698static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008699
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008700/* sysfs fan fan2_input ------------------------------------------------ */
8701static ssize_t fan_fan2_input_show(struct device *dev,
8702 struct device_attribute *attr,
8703 char *buf)
8704{
8705 int res;
8706 unsigned int speed;
8707
8708 res = fan2_get_speed(&speed);
8709 if (res < 0)
8710 return res;
8711
Ye Guojin33ce79b2021-10-18 09:17:50 +00008712 return sysfs_emit(buf, "%u\n", speed);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008713}
8714
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008715static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008716
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008717/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008718static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008719{
Ye Guojin33ce79b2021-10-18 09:17:50 +00008720 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008721}
8722
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008723static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8724 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008725{
8726 unsigned long t;
8727
8728 if (parse_strtoul(buf, 120, &t))
8729 return -EINVAL;
8730
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008731 if (!fan_control_allowed)
8732 return -EPERM;
8733
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008734 fan_watchdog_maxinterval = t;
8735 fan_watchdog_reset();
8736
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008737 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8738
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008739 return count;
8740}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008741static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008742
8743/* --------------------------------------------------------------------- */
8744static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008745 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8746 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008747 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008748 NULL
8749};
8750
8751static const struct attribute_group fan_attr_group = {
8752 .attrs = fan_attributes,
8753};
8754
Jouke Witteveen599eeff2018-12-05 12:17:52 +01008755#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008756#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Lars Hofhansl14232c62020-04-23 14:57:09 -07008757#define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008758
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008759static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
Jouke Witteveen599eeff2018-12-05 12:17:52 +01008760 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8761 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8762 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8763 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8764 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8765 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
Lars Hofhansl14232c62020-04-23 14:57:09 -07008766 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8767 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8768 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8769 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8770 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
Jeannie Stevensonc47c0422021-01-15 16:06:30 +00008771 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
Lars Hofhansl14232c62020-04-23 14:57:09 -07008772 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8773 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
Iakov 'Jake' Kirilenkoc758be82020-11-05 18:25:56 +03008774 TPACPI_Q_LNV3('N', '2', 'V', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (3nd gen) */
Jimmy Wang1f338952021-11-05 17:05:28 +08008775 TPACPI_Q_LNV3('N', '4', '0', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (4nd gen) */
Matthias Maiere8637df2020-11-25 18:04:16 -06008776 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
Til Jasper Ullrich25acf212021-05-25 17:09:52 +02008777 TPACPI_Q_LNV3('N', '3', '2', TPACPI_FAN_2CTL), /* X1 Carbon (9th gen) */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008778};
8779
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008780static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008781{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008782 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008783 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008784
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008785 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8786 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008787
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008788 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008789 fan_status_access_mode = TPACPI_FAN_NONE;
8790 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008791 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008792 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008793 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008794 tp_features.second_fan = 0;
Lars Hofhansl14232c62020-04-23 14:57:09 -07008795 tp_features.second_fan_ctl = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008796 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008797
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008798 if (tpacpi_is_ibm()) {
8799 TPACPI_ACPIHANDLE_INIT(fans);
8800 TPACPI_ACPIHANDLE_INIT(gfan);
8801 TPACPI_ACPIHANDLE_INIT(sfan);
8802 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008803
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008804 quirks = tpacpi_check_quirks(fan_quirk_table,
8805 ARRAY_SIZE(fan_quirk_table));
8806
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008807 if (gfan_handle) {
8808 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008809 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008810 } else {
8811 /* all other ThinkPads: note that even old-style
8812 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008813 if (likely(acpi_ec_read(fan_status_offset,
8814 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008815 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008816 if (quirks & TPACPI_FAN_Q1)
8817 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008818 if (quirks & TPACPI_FAN_2FAN) {
8819 tp_features.second_fan = 1;
Lars Hofhansl14232c62020-04-23 14:57:09 -07008820 pr_info("secondary fan support enabled\n");
8821 }
8822 if (quirks & TPACPI_FAN_2CTL) {
8823 tp_features.second_fan = 1;
8824 tp_features.second_fan_ctl = 1;
8825 pr_info("secondary fan control enabled\n");
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008826 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008827 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008828 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008829 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008830 }
8831 }
8832
8833 if (sfan_handle) {
8834 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008835 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008836 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008837 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008838 } else {
8839 if (!gfan_handle) {
8840 /* gfan without sfan means no fan control */
8841 /* all other models implement TP EC 0x2f control */
8842
8843 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008844 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008845 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008846 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008847 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008848 TPACPI_FAN_CMD_SPEED |
8849 TPACPI_FAN_CMD_LEVEL |
8850 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008851 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008852 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008853 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008854 TPACPI_FAN_CMD_LEVEL |
8855 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008856 }
8857 }
8858 }
8859
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008860 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8861 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008862 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8863 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8864 fan_status_access_mode, fan_control_access_mode);
8865
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008866 /* fan control master switch */
8867 if (!fan_control_allowed) {
8868 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8869 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008870 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008871 "fan control features disabled by parameter\n");
8872 }
8873
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008874 /* update fan_control_desired_level */
8875 if (fan_status_access_mode != TPACPI_FAN_NONE)
8876 fan_get_status_safe(NULL);
8877
8878 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8879 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008880 if (tp_features.second_fan) {
8881 /* attach second fan tachometer */
8882 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008883 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008884 }
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008885 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008886 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008887 if (rc < 0)
8888 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008889
8890 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8891 &driver_attr_fan_watchdog);
8892 if (rc < 0) {
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008893 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008894 &fan_attr_group);
8895 return rc;
8896 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008897 return 0;
8898 } else
8899 return 1;
8900}
8901
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008902static void fan_exit(void)
8903{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008904 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008905 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008906
8907 /* FIXME: can we really do this unconditionally? */
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008908 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008909 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8910 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008911
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008912 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008913 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008914}
8915
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008916static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008917{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008918 int rc;
8919
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008920 if (!fan_control_allowed)
8921 return;
8922
8923 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008924 fan_control_resume_level = 0;
8925 rc = fan_get_status_safe(&fan_control_resume_level);
8926 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008927 pr_notice("failed to read fan level for later restore during resume: %d\n",
8928 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008929
8930 /* if it is undefined, don't attempt to restore it.
8931 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008932 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008933 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008934}
8935
8936static void fan_resume(void)
8937{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008938 u8 current_level = 7;
8939 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008940 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008941
8942 /* DSDT *always* updates status on resume */
8943 tp_features.fan_ctrl_status_undef = 0;
8944
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008945 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008946 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008947 (fan_get_status_safe(&current_level) < 0))
8948 return;
8949
8950 switch (fan_control_access_mode) {
8951 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008952 /* never decrease fan level */
8953 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008954 break;
8955 case TPACPI_FAN_WR_ACPI_FANS:
8956 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008957 /* never decrease fan level, scale is:
8958 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8959 *
8960 * We expect the firmware to set either 7 or AUTO, but we
8961 * handle FULLSPEED out of paranoia.
8962 *
8963 * So, we can safely only restore FULLSPEED or 7, anything
8964 * else could slow the fan. Restoring AUTO is useless, at
8965 * best that's exactly what the DSDT already set (it is the
8966 * slower it uses).
8967 *
8968 * Always keep in mind that the DSDT *will* have set the
8969 * fans to what the vendor supposes is the best level. We
8970 * muck with it only to speed the fan up.
8971 */
8972 if (fan_control_resume_level != 7 &&
8973 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8974 return;
8975 else
8976 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8977 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008978 break;
8979 default:
8980 return;
8981 }
8982 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008983 pr_notice("restoring fan level to 0x%02x\n",
8984 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008985 rc = fan_set_level_safe(fan_control_resume_level);
8986 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008987 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008988 }
8989}
8990
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008991static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008992{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008993 int rc;
8994 u8 status;
8995 unsigned int speed = 0;
8996
8997 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008998 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008999 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009000 rc = fan_get_status_safe(&status);
9001 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009002 return rc;
9003
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009004 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009005 "level:\t\t%d\n",
9006 (status != 0) ? "enabled" : "disabled", status);
9007 break;
9008
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009009 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009010 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009011 rc = fan_get_status_safe(&status);
9012 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009013 return rc;
9014
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009015 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009016 (status != 0) ? "enabled" : "disabled");
9017
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009018 rc = fan_get_speed(&speed);
9019 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009020 return rc;
9021
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009022 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009023
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009024 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009025 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009026 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009027 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009028 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009029 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009030 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009031 break;
9032
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009033 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009034 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009035 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009036 }
9037
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009038 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009039 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009040
9041 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009042 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009043 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009044 break;
9045
9046 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009047 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009048 break;
9049 }
9050 }
9051
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009052 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009053 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009054 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009055
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009056 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009057 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009058
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009059 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009060}
9061
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009062static int fan_write_cmd_level(const char *cmd, int *rc)
9063{
9064 int level;
9065
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009066 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009067 level = TP_EC_FAN_AUTO;
Nathan Chancellorfd96e352021-10-18 11:25:37 -07009068 else if ((strlencmp(cmd, "level disengaged") == 0) ||
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009069 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009070 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009071 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009072 return 0;
9073
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009074 *rc = fan_set_level_safe(level);
9075 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009076 pr_err("level command accepted for unsupported access mode %d\n",
9077 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009078 else if (!*rc)
9079 tpacpi_disclose_usertask("procfs fan",
9080 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009081
9082 return 1;
9083}
9084
9085static int fan_write_cmd_enable(const char *cmd, int *rc)
9086{
9087 if (strlencmp(cmd, "enable") != 0)
9088 return 0;
9089
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009090 *rc = fan_set_enable();
9091 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009092 pr_err("enable command accepted for unsupported access mode %d\n",
9093 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009094 else if (!*rc)
9095 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009096
9097 return 1;
9098}
9099
9100static int fan_write_cmd_disable(const char *cmd, int *rc)
9101{
9102 if (strlencmp(cmd, "disable") != 0)
9103 return 0;
9104
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009105 *rc = fan_set_disable();
9106 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009107 pr_err("disable command accepted for unsupported access mode %d\n",
9108 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009109 else if (!*rc)
9110 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009111
9112 return 1;
9113}
9114
9115static int fan_write_cmd_speed(const char *cmd, int *rc)
9116{
9117 int speed;
9118
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009119 /* TODO:
9120 * Support speed <low> <medium> <high> ? */
9121
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009122 if (sscanf(cmd, "speed %d", &speed) != 1)
9123 return 0;
9124
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009125 *rc = fan_set_speed(speed);
9126 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009127 pr_err("speed command accepted for unsupported access mode %d\n",
9128 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009129 else if (!*rc)
9130 tpacpi_disclose_usertask("procfs fan",
9131 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009132
9133 return 1;
9134}
9135
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009136static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9137{
9138 int interval;
9139
9140 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9141 return 0;
9142
9143 if (interval < 0 || interval > 120)
9144 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009145 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009146 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009147 tpacpi_disclose_usertask("procfs fan",
9148 "set watchdog timer to %d\n",
9149 interval);
9150 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009151
9152 return 1;
9153}
9154
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009155static int fan_write(char *buf)
9156{
9157 char *cmd;
9158 int rc = 0;
9159
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03009160 while (!rc && (cmd = strsep(&buf, ","))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009161 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009162 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009163 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009164 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009165 fan_write_cmd_disable(cmd, &rc) ||
9166 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009167 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009168 fan_write_cmd_speed(cmd, &rc))
9169 )
9170 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009171 else if (!rc)
9172 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009173 }
9174
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009175 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009176}
9177
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009178static struct ibm_struct fan_driver_data = {
9179 .name = "fan",
9180 .read = fan_read,
9181 .write = fan_write,
9182 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009183 .suspend = fan_suspend,
9184 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009185};
9186
David Henningsson420f9732013-10-16 23:10:31 +02009187/*************************************************************************
9188 * Mute LED subdriver
9189 */
9190
Takashi Iwai9e908a12018-11-26 17:47:47 +01009191#define TPACPI_LED_MAX 2
David Henningsson420f9732013-10-16 23:10:31 +02009192
9193struct tp_led_table {
9194 acpi_string name;
9195 int on_value;
9196 int off_value;
9197 int state;
9198};
9199
Takashi Iwai9e908a12018-11-26 17:47:47 +01009200static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9201 [LED_AUDIO_MUTE] = {
David Henningsson420f9732013-10-16 23:10:31 +02009202 .name = "SSMS",
9203 .on_value = 1,
9204 .off_value = 0,
9205 },
Takashi Iwai9e908a12018-11-26 17:47:47 +01009206 [LED_AUDIO_MICMUTE] = {
David Henningsson420f9732013-10-16 23:10:31 +02009207 .name = "MMTS",
9208 .on_value = 2,
9209 .off_value = 0,
9210 },
9211};
9212
9213static int mute_led_on_off(struct tp_led_table *t, bool state)
9214{
9215 acpi_handle temp;
9216 int output;
9217
Axel Lin3526eca2016-09-19 09:33:51 +08009218 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009219 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9220 return -EIO;
9221 }
9222
9223 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9224 state ? t->on_value : t->off_value))
9225 return -EIO;
9226
9227 t->state = state;
9228 return state;
9229}
9230
Takashi Iwai9e908a12018-11-26 17:47:47 +01009231static int tpacpi_led_set(int whichled, bool on)
David Henningsson420f9732013-10-16 23:10:31 +02009232{
9233 struct tp_led_table *t;
9234
David Henningsson420f9732013-10-16 23:10:31 +02009235 t = &led_tables[whichled];
9236 if (t->state < 0 || t->state == on)
9237 return t->state;
9238 return mute_led_on_off(t, on);
9239}
David Henningsson420f9732013-10-16 23:10:31 +02009240
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009241static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9242 enum led_brightness brightness)
9243{
Takashi Iwai9e908a12018-11-26 17:47:47 +01009244 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009245}
9246
9247static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9248 enum led_brightness brightness)
9249{
Takashi Iwai9e908a12018-11-26 17:47:47 +01009250 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009251}
9252
Takashi Iwai9e908a12018-11-26 17:47:47 +01009253static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9254 [LED_AUDIO_MUTE] = {
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009255 .name = "platform::mute",
9256 .max_brightness = 1,
9257 .brightness_set_blocking = tpacpi_led_mute_set,
9258 .default_trigger = "audio-mute",
9259 },
Takashi Iwai9e908a12018-11-26 17:47:47 +01009260 [LED_AUDIO_MICMUTE] = {
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009261 .name = "platform::micmute",
9262 .max_brightness = 1,
9263 .brightness_set_blocking = tpacpi_led_micmute_set,
9264 .default_trigger = "audio-micmute",
9265 },
9266};
David Henningsson420f9732013-10-16 23:10:31 +02009267
9268static int mute_led_init(struct ibm_init_struct *iibm)
9269{
9270 acpi_handle temp;
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009271 int i, err;
David Henningsson420f9732013-10-16 23:10:31 +02009272
9273 for (i = 0; i < TPACPI_LED_MAX; i++) {
9274 struct tp_led_table *t = &led_tables[i];
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009275 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009276 t->state = -ENODEV;
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009277 continue;
9278 }
9279
Takashi Iwai9e908a12018-11-26 17:47:47 +01009280 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009281 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9282 if (err < 0) {
Andy Shevchenko41f80042020-05-07 19:37:23 +03009283 while (i--)
9284 led_classdev_unregister(&mute_led_cdev[i]);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009285 return err;
9286 }
David Henningsson420f9732013-10-16 23:10:31 +02009287 }
9288 return 0;
9289}
9290
9291static void mute_led_exit(void)
9292{
9293 int i;
9294
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009295 for (i = 0; i < TPACPI_LED_MAX; i++) {
Andy Shevchenko41f80042020-05-07 19:37:23 +03009296 led_classdev_unregister(&mute_led_cdev[i]);
9297 tpacpi_led_set(i, false);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009298 }
David Henningsson420f9732013-10-16 23:10:31 +02009299}
9300
Takashi Iwai119f4492014-02-12 16:32:45 +01009301static void mute_led_resume(void)
9302{
9303 int i;
9304
9305 for (i = 0; i < TPACPI_LED_MAX; i++) {
9306 struct tp_led_table *t = &led_tables[i];
9307 if (t->state >= 0)
9308 mute_led_on_off(t, t->state);
9309 }
9310}
9311
David Henningsson420f9732013-10-16 23:10:31 +02009312static struct ibm_struct mute_led_driver_data = {
9313 .name = "mute_led",
9314 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009315 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009316};
9317
Ognjen Galic2801b962018-02-07 15:58:44 +01009318/*
9319 * Battery Wear Control Driver
9320 * Contact: Ognjen Galic <smclt30p@gmail.com>
9321 */
9322
9323/* Metadata */
9324
9325#define GET_START "BCTG"
9326#define SET_START "BCCS"
9327#define GET_STOP "BCSG"
9328#define SET_STOP "BCSS"
9329
Ognjen Galic2801b962018-02-07 15:58:44 +01009330enum {
9331 BAT_ANY = 0,
9332 BAT_PRIMARY = 1,
9333 BAT_SECONDARY = 2
9334};
9335
9336enum {
9337 /* Error condition bit */
9338 METHOD_ERR = BIT(31),
9339};
9340
9341enum {
9342 /* This is used in the get/set helpers */
9343 THRESHOLD_START,
9344 THRESHOLD_STOP,
9345};
9346
9347struct tpacpi_battery_data {
9348 int charge_start;
9349 int start_support;
9350 int charge_stop;
9351 int stop_support;
9352};
9353
9354struct tpacpi_battery_driver_data {
9355 struct tpacpi_battery_data batteries[3];
9356 int individual_addressing;
9357};
9358
9359static struct tpacpi_battery_driver_data battery_info;
9360
9361/* ACPI helpers/functions/probes */
9362
9363/**
9364 * This evaluates a ACPI method call specific to the battery
9365 * ACPI extension. The specifics are that an error is marked
9366 * in the 32rd bit of the response, so we just check that here.
9367 */
9368static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9369{
9370 int response;
9371
9372 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9373 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9374 return AE_ERROR;
9375 }
9376 if (response & METHOD_ERR) {
9377 acpi_handle_err(hkey_handle,
9378 "%s evaluated but flagged as error", method);
9379 return AE_ERROR;
9380 }
9381 *ret = response;
9382 return AE_OK;
9383}
9384
9385static int tpacpi_battery_get(int what, int battery, int *ret)
9386{
9387 switch (what) {
9388 case THRESHOLD_START:
9389 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9390 return -ENODEV;
9391
9392 /* The value is in the low 8 bits of the response */
9393 *ret = *ret & 0xFF;
9394 return 0;
9395 case THRESHOLD_STOP:
9396 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9397 return -ENODEV;
9398 /* Value is in lower 8 bits */
9399 *ret = *ret & 0xFF;
9400 /*
9401 * On the stop value, if we return 0 that
9402 * does not make any sense. 0 means Default, which
9403 * means that charging stops at 100%, so we return
9404 * that.
9405 */
9406 if (*ret == 0)
9407 *ret = 100;
9408 return 0;
9409 default:
9410 pr_crit("wrong parameter: %d", what);
9411 return -EINVAL;
9412 }
9413}
9414
9415static int tpacpi_battery_set(int what, int battery, int value)
9416{
9417 int param, ret;
9418 /* The first 8 bits are the value of the threshold */
9419 param = value;
9420 /* The battery ID is in bits 8-9, 2 bits */
9421 param |= battery << 8;
9422
9423 switch (what) {
9424 case THRESHOLD_START:
9425 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9426 pr_err("failed to set charge threshold on battery %d",
9427 battery);
9428 return -ENODEV;
9429 }
9430 return 0;
9431 case THRESHOLD_STOP:
9432 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9433 pr_err("failed to set stop threshold: %d", battery);
9434 return -ENODEV;
9435 }
9436 return 0;
9437 default:
9438 pr_crit("wrong parameter: %d", what);
9439 return -EINVAL;
9440 }
9441}
9442
9443static int tpacpi_battery_probe(int battery)
9444{
9445 int ret = 0;
9446
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009447 memset(&battery_info.batteries[battery], 0,
9448 sizeof(battery_info.batteries[battery]));
9449
Ognjen Galic2801b962018-02-07 15:58:44 +01009450 /*
9451 * 1) Get the current start threshold
9452 * 2) Check for support
9453 * 3) Get the current stop threshold
9454 * 4) Check for support
9455 */
9456 if (acpi_has_method(hkey_handle, GET_START)) {
9457 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9458 pr_err("Error probing battery %d\n", battery);
9459 return -ENODEV;
9460 }
9461 /* Individual addressing is in bit 9 */
9462 if (ret & BIT(9))
9463 battery_info.individual_addressing = true;
9464 /* Support is marked in bit 8 */
9465 if (ret & BIT(8))
9466 battery_info.batteries[battery].start_support = 1;
9467 else
9468 return -ENODEV;
9469 if (tpacpi_battery_get(THRESHOLD_START, battery,
9470 &battery_info.batteries[battery].charge_start)) {
9471 pr_err("Error probing battery %d\n", battery);
9472 return -ENODEV;
9473 }
9474 }
9475 if (acpi_has_method(hkey_handle, GET_STOP)) {
9476 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9477 pr_err("Error probing battery stop; %d\n", battery);
9478 return -ENODEV;
9479 }
9480 /* Support is marked in bit 8 */
9481 if (ret & BIT(8))
9482 battery_info.batteries[battery].stop_support = 1;
9483 else
9484 return -ENODEV;
9485 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9486 &battery_info.batteries[battery].charge_stop)) {
9487 pr_err("Error probing battery stop: %d\n", battery);
9488 return -ENODEV;
9489 }
9490 }
9491 pr_info("battery %d registered (start %d, stop %d)",
9492 battery,
9493 battery_info.batteries[battery].charge_start,
9494 battery_info.batteries[battery].charge_stop);
9495
9496 return 0;
9497}
9498
9499/* General helper functions */
9500
9501static int tpacpi_battery_get_id(const char *battery_name)
9502{
9503
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009504 if (strcmp(battery_name, "BAT0") == 0 ||
9505 tp_features.battery_force_primary)
Ognjen Galic2801b962018-02-07 15:58:44 +01009506 return BAT_PRIMARY;
9507 if (strcmp(battery_name, "BAT1") == 0)
9508 return BAT_SECONDARY;
9509 /*
9510 * If for some reason the battery is not BAT0 nor is it
9511 * BAT1, we will assume it's the default, first battery,
9512 * AKA primary.
9513 */
9514 pr_warn("unknown battery %s, assuming primary", battery_name);
9515 return BAT_PRIMARY;
9516}
9517
9518/* sysfs interface */
9519
9520static ssize_t tpacpi_battery_store(int what,
9521 struct device *dev,
9522 const char *buf, size_t count)
9523{
9524 struct power_supply *supply = to_power_supply(dev);
9525 unsigned long value;
9526 int battery, rval;
9527 /*
9528 * Some systems have support for more than
9529 * one battery. If that is the case,
9530 * tpacpi_battery_probe marked that addressing
9531 * them individually is supported, so we do that
9532 * based on the device struct.
9533 *
9534 * On systems that are not supported, we assume
9535 * the primary as most of the ACPI calls fail
9536 * with "Any Battery" as the parameter.
9537 */
9538 if (battery_info.individual_addressing)
9539 /* BAT_PRIMARY or BAT_SECONDARY */
9540 battery = tpacpi_battery_get_id(supply->desc->name);
9541 else
9542 battery = BAT_PRIMARY;
9543
9544 rval = kstrtoul(buf, 10, &value);
9545 if (rval)
9546 return rval;
9547
9548 switch (what) {
9549 case THRESHOLD_START:
9550 if (!battery_info.batteries[battery].start_support)
9551 return -ENODEV;
9552 /* valid values are [0, 99] */
Xiongfeng Wangb9911782020-04-29 16:59:40 +08009553 if (value > 99)
Ognjen Galic2801b962018-02-07 15:58:44 +01009554 return -EINVAL;
9555 if (value > battery_info.batteries[battery].charge_stop)
9556 return -EINVAL;
9557 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9558 return -ENODEV;
9559 battery_info.batteries[battery].charge_start = value;
9560 return count;
9561
9562 case THRESHOLD_STOP:
9563 if (!battery_info.batteries[battery].stop_support)
9564 return -ENODEV;
9565 /* valid values are [1, 100] */
9566 if (value < 1 || value > 100)
9567 return -EINVAL;
9568 if (value < battery_info.batteries[battery].charge_start)
9569 return -EINVAL;
9570 battery_info.batteries[battery].charge_stop = value;
9571 /*
9572 * When 100 is passed to stop, we need to flip
9573 * it to 0 as that the EC understands that as
9574 * "Default", which will charge to 100%
9575 */
9576 if (value == 100)
9577 value = 0;
9578 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9579 return -EINVAL;
9580 return count;
9581 default:
9582 pr_crit("Wrong parameter: %d", what);
9583 return -EINVAL;
9584 }
9585 return count;
9586}
9587
9588static ssize_t tpacpi_battery_show(int what,
9589 struct device *dev,
9590 char *buf)
9591{
9592 struct power_supply *supply = to_power_supply(dev);
9593 int ret, battery;
9594 /*
9595 * Some systems have support for more than
9596 * one battery. If that is the case,
9597 * tpacpi_battery_probe marked that addressing
9598 * them individually is supported, so we;
9599 * based on the device struct.
9600 *
9601 * On systems that are not supported, we assume
9602 * the primary as most of the ACPI calls fail
9603 * with "Any Battery" as the parameter.
9604 */
9605 if (battery_info.individual_addressing)
9606 /* BAT_PRIMARY or BAT_SECONDARY */
9607 battery = tpacpi_battery_get_id(supply->desc->name);
9608 else
9609 battery = BAT_PRIMARY;
9610 if (tpacpi_battery_get(what, battery, &ret))
9611 return -ENODEV;
9612 return sprintf(buf, "%d\n", ret);
9613}
9614
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009615static ssize_t charge_control_start_threshold_show(struct device *device,
Ognjen Galic2801b962018-02-07 15:58:44 +01009616 struct device_attribute *attr,
9617 char *buf)
9618{
9619 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9620}
9621
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009622static ssize_t charge_control_end_threshold_show(struct device *device,
Ognjen Galic2801b962018-02-07 15:58:44 +01009623 struct device_attribute *attr,
9624 char *buf)
9625{
9626 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9627}
9628
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009629static ssize_t charge_control_start_threshold_store(struct device *dev,
Ognjen Galic2801b962018-02-07 15:58:44 +01009630 struct device_attribute *attr,
9631 const char *buf, size_t count)
9632{
9633 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9634}
9635
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009636static ssize_t charge_control_end_threshold_store(struct device *dev,
Ognjen Galic2801b962018-02-07 15:58:44 +01009637 struct device_attribute *attr,
9638 const char *buf, size_t count)
9639{
9640 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9641}
9642
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009643static DEVICE_ATTR_RW(charge_control_start_threshold);
9644static DEVICE_ATTR_RW(charge_control_end_threshold);
Wei Yongjun066d7f32020-07-29 23:59:14 +08009645static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009646 charge_start_threshold,
9647 0644,
9648 charge_control_start_threshold_show,
9649 charge_control_start_threshold_store
9650);
Wei Yongjun066d7f32020-07-29 23:59:14 +08009651static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009652 charge_stop_threshold,
9653 0644,
9654 charge_control_end_threshold_show,
9655 charge_control_end_threshold_store
9656);
Ognjen Galic2801b962018-02-07 15:58:44 +01009657
9658static struct attribute *tpacpi_battery_attrs[] = {
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009659 &dev_attr_charge_control_start_threshold.attr,
9660 &dev_attr_charge_control_end_threshold.attr,
Ognjen Galic2801b962018-02-07 15:58:44 +01009661 &dev_attr_charge_start_threshold.attr,
9662 &dev_attr_charge_stop_threshold.attr,
9663 NULL,
9664};
9665
9666ATTRIBUTE_GROUPS(tpacpi_battery);
9667
9668/* ACPI battery hooking */
9669
9670static int tpacpi_battery_add(struct power_supply *battery)
9671{
9672 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9673
9674 if (tpacpi_battery_probe(batteryid))
9675 return -ENODEV;
9676 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9677 return -ENODEV;
9678 return 0;
9679}
9680
9681static int tpacpi_battery_remove(struct power_supply *battery)
9682{
9683 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9684 return 0;
9685}
9686
9687static struct acpi_battery_hook battery_hook = {
9688 .add_battery = tpacpi_battery_add,
9689 .remove_battery = tpacpi_battery_remove,
9690 .name = "ThinkPad Battery Extension",
9691};
9692
9693/* Subdriver init/exit */
9694
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009695static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9696 /*
9697 * Individual addressing is broken on models that expose the
9698 * primary battery as BAT1.
9699 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009700 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9701 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9702 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009703 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009704 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
Hans de Goede3cd420b2020-11-09 11:35:50 +01009705 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009706};
9707
Ognjen Galic2801b962018-02-07 15:58:44 +01009708static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9709{
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009710 memset(&battery_info, 0, sizeof(battery_info));
9711
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009712 tp_features.battery_force_primary = tpacpi_check_quirks(
9713 battery_quirk_table,
9714 ARRAY_SIZE(battery_quirk_table));
9715
Ognjen Galic2801b962018-02-07 15:58:44 +01009716 battery_hook_register(&battery_hook);
9717 return 0;
9718}
9719
9720static void tpacpi_battery_exit(void)
9721{
9722 battery_hook_unregister(&battery_hook);
9723}
9724
9725static struct ibm_struct battery_driver_data = {
9726 .name = "battery",
9727 .exit = tpacpi_battery_exit,
9728};
9729
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009730/*************************************************************************
9731 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9732 */
9733
Hans de Goedef8098912021-10-05 22:23:20 +02009734static struct drm_privacy_screen *lcdshadow_dev;
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009735static acpi_handle lcdshadow_get_handle;
9736static acpi_handle lcdshadow_set_handle;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009737
Hans de Goedef8098912021-10-05 22:23:20 +02009738static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
9739 enum drm_privacy_screen_status state)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009740{
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009741 int output;
9742
Hans de Goedef8098912021-10-05 22:23:20 +02009743 if (WARN_ON(!mutex_is_locked(&priv->lock)))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009744 return -EIO;
9745
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009746 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009747 return -EIO;
9748
Hans de Goedef8098912021-10-05 22:23:20 +02009749 priv->hw_state = priv->sw_state = state;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009750 return 0;
9751}
9752
Hans de Goedef8098912021-10-05 22:23:20 +02009753static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009754{
Hans de Goedef8098912021-10-05 22:23:20 +02009755 int output;
9756
9757 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
9758 return;
9759
9760 priv->hw_state = priv->sw_state = output & 0x1;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009761}
9762
Hans de Goedef8098912021-10-05 22:23:20 +02009763static const struct drm_privacy_screen_ops lcdshadow_ops = {
9764 .set_sw_state = lcdshadow_set_sw_state,
9765 .get_hw_state = lcdshadow_get_hw_state,
9766};
9767
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009768static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9769{
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009770 acpi_status status1, status2;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009771 int output;
9772
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009773 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
9774 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
Hans de Goedef8098912021-10-05 22:23:20 +02009775 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009776 return 0;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009777
Hans de Goedef8098912021-10-05 22:23:20 +02009778 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009779 return -EIO;
Hans de Goedef8098912021-10-05 22:23:20 +02009780
9781 if (!(output & 0x10000))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009782 return 0;
Hans de Goedef8098912021-10-05 22:23:20 +02009783
9784 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
9785 &lcdshadow_ops);
9786 if (IS_ERR(lcdshadow_dev))
9787 return PTR_ERR(lcdshadow_dev);
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009788
9789 return 0;
9790}
9791
Hans de Goedef8098912021-10-05 22:23:20 +02009792static void lcdshadow_exit(void)
9793{
9794 drm_privacy_screen_unregister(lcdshadow_dev);
9795}
9796
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009797static void lcdshadow_resume(void)
9798{
Hans de Goedef8098912021-10-05 22:23:20 +02009799 if (!lcdshadow_dev)
9800 return;
9801
9802 mutex_lock(&lcdshadow_dev->lock);
9803 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
9804 mutex_unlock(&lcdshadow_dev->lock);
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009805}
9806
9807static int lcdshadow_read(struct seq_file *m)
9808{
Hans de Goedef8098912021-10-05 22:23:20 +02009809 if (!lcdshadow_dev) {
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009810 seq_puts(m, "status:\t\tnot supported\n");
9811 } else {
Hans de Goedef8098912021-10-05 22:23:20 +02009812 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009813 seq_puts(m, "commands:\t0, 1\n");
9814 }
9815
9816 return 0;
9817}
9818
9819static int lcdshadow_write(char *buf)
9820{
9821 char *cmd;
Andy Shevchenko466f4692020-05-11 15:54:14 +03009822 int res, state = -EINVAL;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009823
Hans de Goedef8098912021-10-05 22:23:20 +02009824 if (!lcdshadow_dev)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009825 return -ENODEV;
9826
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03009827 while ((cmd = strsep(&buf, ","))) {
Andy Shevchenko466f4692020-05-11 15:54:14 +03009828 res = kstrtoint(cmd, 10, &state);
9829 if (res < 0)
9830 return res;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009831 }
9832
Andy Shevchenko466f4692020-05-11 15:54:14 +03009833 if (state >= 2 || state < 0)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009834 return -EINVAL;
9835
Hans de Goedef8098912021-10-05 22:23:20 +02009836 mutex_lock(&lcdshadow_dev->lock);
9837 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
9838 mutex_unlock(&lcdshadow_dev->lock);
9839
9840 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
9841
9842 return res;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009843}
9844
9845static struct ibm_struct lcdshadow_driver_data = {
9846 .name = "lcdshadow",
Hans de Goedef8098912021-10-05 22:23:20 +02009847 .exit = lcdshadow_exit,
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009848 .resume = lcdshadow_resume,
9849 .read = lcdshadow_read,
9850 .write = lcdshadow_write,
9851};
9852
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009853/*************************************************************************
Mark Pearson1ac09652020-11-24 13:11:54 -05009854 * Thinkpad sensor interfaces
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009855 */
9856
Mark Pearson0b4f5c92021-03-11 12:48:43 -05009857#define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
9858#define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
9859#define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
9860#define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
9861
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009862#define DYTC_CMD_GET 2 /* To get current IC function and mode */
9863#define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
9864
Mark Pearson1ac09652020-11-24 13:11:54 -05009865#define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
9866#define PALMSENSOR_ON_BIT 1 /* psensor status */
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009867
Mark Pearson1ac09652020-11-24 13:11:54 -05009868static bool has_palmsensor;
9869static bool has_lapsensor;
9870static bool palm_state;
9871static bool lap_state;
Mark Pearson0b4f5c92021-03-11 12:48:43 -05009872static int dytc_version;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009873
Mark Pearsonc3bfcd42021-01-11 11:22:37 -05009874static int dytc_command(int command, int *output)
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009875{
9876 acpi_handle dytc_handle;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -05009877
9878 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
9879 /* Platform doesn't support DYTC */
9880 return -ENODEV;
9881 }
9882 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
9883 return -EIO;
9884 return 0;
9885}
9886
Mark Pearson0b4f5c92021-03-11 12:48:43 -05009887static int dytc_get_version(void)
9888{
9889 int err, output;
9890
9891 /* Check if we've been called before - and just return cached value */
9892 if (dytc_version)
9893 return dytc_version;
9894
9895 /* Otherwise query DYTC and extract version information */
9896 err = dytc_command(DYTC_CMD_QUERY, &output);
9897 /*
9898 * If support isn't available (ENODEV) then don't return an error
9899 * and don't create the sysfs group
9900 */
9901 if (err == -ENODEV)
9902 return 0;
9903 /* For all other errors we can flag the failure */
9904 if (err)
9905 return err;
9906
9907 /* Check DYTC is enabled and supports mode setting */
9908 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
9909 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
9910
9911 return 0;
9912}
9913
Mark Pearsonc3bfcd42021-01-11 11:22:37 -05009914static int lapsensor_get(bool *present, bool *state)
9915{
9916 int output, err;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009917
Mark Pearson1ac09652020-11-24 13:11:54 -05009918 *present = false;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -05009919 err = dytc_command(DYTC_CMD_GET, &output);
9920 if (err)
9921 return err;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009922
Mark Pearson1ac09652020-11-24 13:11:54 -05009923 *present = true; /*If we get his far, we have lapmode support*/
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009924 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
9925 return 0;
9926}
9927
Mark Pearson1ac09652020-11-24 13:11:54 -05009928static int palmsensor_get(bool *present, bool *state)
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009929{
Mark Pearson1ac09652020-11-24 13:11:54 -05009930 acpi_handle psensor_handle;
9931 int output;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009932
Mark Pearson1ac09652020-11-24 13:11:54 -05009933 *present = false;
9934 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
9935 return -ENODEV;
9936 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
9937 return -EIO;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009938
Mark Pearson1ac09652020-11-24 13:11:54 -05009939 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
9940 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
9941 return 0;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009942}
9943
Mark Pearson1ac09652020-11-24 13:11:54 -05009944static void lapsensor_refresh(void)
9945{
9946 bool state;
9947 int err;
9948
9949 if (has_lapsensor) {
9950 err = lapsensor_get(&has_lapsensor, &state);
9951 if (err)
9952 return;
9953 if (lap_state != state) {
9954 lap_state = state;
9955 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
9956 }
9957 }
9958}
9959
9960static void palmsensor_refresh(void)
9961{
9962 bool state;
9963 int err;
9964
9965 if (has_palmsensor) {
9966 err = palmsensor_get(&has_palmsensor, &state);
9967 if (err)
9968 return;
9969 if (palm_state != state) {
9970 palm_state = state;
9971 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
9972 }
9973 }
9974}
9975
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009976static ssize_t dytc_lapmode_show(struct device *dev,
9977 struct device_attribute *attr,
9978 char *buf)
9979{
Mark Pearson1ac09652020-11-24 13:11:54 -05009980 if (has_lapsensor)
9981 return sysfs_emit(buf, "%d\n", lap_state);
9982 return sysfs_emit(buf, "\n");
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009983}
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009984static DEVICE_ATTR_RO(dytc_lapmode);
9985
Mark Pearson1ac09652020-11-24 13:11:54 -05009986static ssize_t palmsensor_show(struct device *dev,
9987 struct device_attribute *attr,
9988 char *buf)
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009989{
Mark Pearson1ac09652020-11-24 13:11:54 -05009990 if (has_palmsensor)
9991 return sysfs_emit(buf, "%d\n", palm_state);
9992 return sysfs_emit(buf, "\n");
9993}
9994static DEVICE_ATTR_RO(palmsensor);
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009995
Mark Pearson1ac09652020-11-24 13:11:54 -05009996static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
9997{
9998 int palm_err, lap_err, err;
9999
10000 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10001 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10002 /*
10003 * If support isn't available (ENODEV) for both devices then quit, but
10004 * don't return an error.
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010005 */
Mark Pearson1ac09652020-11-24 13:11:54 -050010006 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010007 return 0;
Mark Pearson1ac09652020-11-24 13:11:54 -050010008 /* Otherwise, if there was an error return it */
Mark Pearsonaa44afa2020-12-29 21:47:26 -050010009 if (palm_err && (palm_err != -ENODEV))
Mark Pearson1ac09652020-11-24 13:11:54 -050010010 return palm_err;
Mark Pearsonaa44afa2020-12-29 21:47:26 -050010011 if (lap_err && (lap_err != -ENODEV))
Mark Pearson1ac09652020-11-24 13:11:54 -050010012 return lap_err;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010013
Mark Pearson1ac09652020-11-24 13:11:54 -050010014 if (has_palmsensor) {
10015 err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_palmsensor.attr);
10016 if (err)
10017 return err;
10018 }
Mark Pearson0b4f5c92021-03-11 12:48:43 -050010019
10020 /* Check if we know the DYTC version, if we don't then get it */
10021 if (!dytc_version) {
10022 err = dytc_get_version();
10023 if (err)
10024 return err;
10025 }
10026 /*
10027 * Platforms before DYTC version 5 claim to have a lap sensor, but it doesn't work, so we
10028 * ignore them
10029 */
10030 if (has_lapsensor && (dytc_version >= 5)) {
Mark Pearson1ac09652020-11-24 13:11:54 -050010031 err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_dytc_lapmode.attr);
10032 if (err)
10033 return err;
10034 }
10035 return 0;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010036}
10037
Mark Pearson1ac09652020-11-24 13:11:54 -050010038static void proxsensor_exit(void)
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010039{
Mark Pearson1ac09652020-11-24 13:11:54 -050010040 if (has_lapsensor)
10041 sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_dytc_lapmode.attr);
10042 if (has_palmsensor)
10043 sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_palmsensor.attr);
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010044}
10045
Mark Pearson1ac09652020-11-24 13:11:54 -050010046static struct ibm_struct proxsensor_driver_data = {
10047 .name = "proximity-sensor",
10048 .exit = proxsensor_exit,
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010049};
10050
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010051/*************************************************************************
10052 * DYTC Platform Profile interface
10053 */
10054
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010055#define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
Mark Pearson7a47f862021-04-06 19:32:03 -040010056#define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010057#define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10058
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010059#define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10060#define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10061
10062#define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10063#define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10064#define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10065
10066#define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10067#define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10068#define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
10069
10070#define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
10071#define DYTC_MODE_LOWPOWER 3 /* Low power mode */
10072#define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
Mark Pearson7a47f862021-04-06 19:32:03 -040010073#define DYTC_MODE_MMC_BALANCE 0 /* Default mode from MMC_GET, aka balanced */
10074
10075#define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10076#define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010077
10078#define DYTC_SET_COMMAND(function, mode, on) \
10079 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10080 (mode) << DYTC_SET_MODE_BIT | \
10081 (on) << DYTC_SET_VALID_BIT)
10082
10083#define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
10084
10085#define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
10086
10087static bool dytc_profile_available;
10088static enum platform_profile_option dytc_current_profile;
10089static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10090static DEFINE_MUTEX(dytc_mutex);
Mark Pearson7a47f862021-04-06 19:32:03 -040010091static bool dytc_mmc_get_available;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010092
10093static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
10094{
10095 switch (dytcmode) {
10096 case DYTC_MODE_LOWPOWER:
10097 *profile = PLATFORM_PROFILE_LOW_POWER;
10098 break;
10099 case DYTC_MODE_BALANCE:
Mark Pearson7a47f862021-04-06 19:32:03 -040010100 case DYTC_MODE_MMC_BALANCE:
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010101 *profile = PLATFORM_PROFILE_BALANCED;
10102 break;
10103 case DYTC_MODE_PERFORM:
10104 *profile = PLATFORM_PROFILE_PERFORMANCE;
10105 break;
10106 default: /* Unknown mode */
10107 return -EINVAL;
10108 }
10109 return 0;
10110}
10111
10112static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10113{
10114 switch (profile) {
10115 case PLATFORM_PROFILE_LOW_POWER:
10116 *perfmode = DYTC_MODE_LOWPOWER;
10117 break;
10118 case PLATFORM_PROFILE_BALANCED:
10119 *perfmode = DYTC_MODE_BALANCE;
10120 break;
10121 case PLATFORM_PROFILE_PERFORMANCE:
10122 *perfmode = DYTC_MODE_PERFORM;
10123 break;
10124 default: /* Unknown profile */
10125 return -EOPNOTSUPP;
10126 }
10127 return 0;
10128}
10129
10130/*
10131 * dytc_profile_get: Function to register with platform_profile
10132 * handler. Returns current platform profile.
10133 */
Hans de Goeded2386d72021-02-04 15:01:57 +010010134static int dytc_profile_get(struct platform_profile_handler *pprof,
10135 enum platform_profile_option *profile)
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010136{
10137 *profile = dytc_current_profile;
10138 return 0;
10139}
10140
10141/*
10142 * Helper function - check if we are in CQL mode and if we are
10143 * - disable CQL,
10144 * - run the command
10145 * - enable CQL
10146 * If not in CQL mode, just run the command
10147 */
Hans de Goeded2386d72021-02-04 15:01:57 +010010148static int dytc_cql_command(int command, int *output)
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010149{
10150 int err, cmd_err, dummy;
10151 int cur_funcmode;
10152
10153 /* Determine if we are in CQL mode. This alters the commands we do */
10154 err = dytc_command(DYTC_CMD_GET, output);
10155 if (err)
10156 return err;
10157
10158 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10159 /* Check if we're OK to return immediately */
10160 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10161 return 0;
10162
10163 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10164 atomic_inc(&dytc_ignore_event);
10165 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10166 if (err)
10167 return err;
10168 }
10169
10170 cmd_err = dytc_command(command, output);
10171 /* Check return condition after we've restored CQL state */
10172
10173 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10174 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10175 if (err)
10176 return err;
10177 }
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010178 return cmd_err;
10179}
10180
10181/*
10182 * dytc_profile_set: Function to register with platform_profile
10183 * handler. Sets current platform profile.
10184 */
Hans de Goeded2386d72021-02-04 15:01:57 +010010185static int dytc_profile_set(struct platform_profile_handler *pprof,
10186 enum platform_profile_option profile)
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010187{
10188 int output;
10189 int err;
10190
10191 if (!dytc_profile_available)
10192 return -ENODEV;
10193
10194 err = mutex_lock_interruptible(&dytc_mutex);
10195 if (err)
10196 return err;
10197
10198 if (profile == PLATFORM_PROFILE_BALANCED) {
Hans de Goedee157c952021-03-21 12:31:08 +010010199 /*
10200 * To get back to balanced mode we need to issue a reset command.
10201 * Note we still need to disable CQL mode before hand and re-enable
10202 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10203 * stuck at 0 for aprox. 30 minutes.
10204 */
10205 err = dytc_cql_command(DYTC_CMD_RESET, &output);
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010206 if (err)
10207 goto unlock;
10208 } else {
10209 int perfmode;
10210
10211 err = convert_profile_to_dytc(profile, &perfmode);
10212 if (err)
10213 goto unlock;
10214
10215 /* Determine if we are in CQL mode. This alters the commands we do */
10216 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1), &output);
10217 if (err)
10218 goto unlock;
10219 }
10220 /* Success - update current profile */
10221 dytc_current_profile = profile;
10222unlock:
10223 mutex_unlock(&dytc_mutex);
10224 return err;
10225}
10226
10227static void dytc_profile_refresh(void)
10228{
10229 enum platform_profile_option profile;
10230 int output, err;
10231 int perfmode;
10232
10233 mutex_lock(&dytc_mutex);
Mark Pearson7a47f862021-04-06 19:32:03 -040010234 if (dytc_mmc_get_available)
10235 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10236 else
10237 err = dytc_cql_command(DYTC_CMD_GET, &output);
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010238 mutex_unlock(&dytc_mutex);
10239 if (err)
10240 return;
10241
10242 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10243 convert_dytc_to_profile(perfmode, &profile);
10244 if (profile != dytc_current_profile) {
10245 dytc_current_profile = profile;
10246 platform_profile_notify();
10247 }
10248}
10249
10250static struct platform_profile_handler dytc_profile = {
10251 .profile_get = dytc_profile_get,
10252 .profile_set = dytc_profile_set,
10253};
10254
10255static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10256{
10257 int err, output;
10258
10259 /* Setup supported modes */
10260 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10261 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10262 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10263
10264 dytc_profile_available = false;
10265 err = dytc_command(DYTC_CMD_QUERY, &output);
10266 /*
10267 * If support isn't available (ENODEV) then don't return an error
10268 * and don't create the sysfs group
10269 */
10270 if (err == -ENODEV)
10271 return 0;
10272 /* For all other errors we can flag the failure */
10273 if (err)
10274 return err;
10275
Mark Pearson0b4f5c92021-03-11 12:48:43 -050010276 /* Check if we know the DYTC version, if we don't then get it */
10277 if (!dytc_version) {
10278 err = dytc_get_version();
10279 if (err)
10280 return err;
10281 }
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010282 /* Check DYTC is enabled and supports mode setting */
Mark Pearson0b4f5c92021-03-11 12:48:43 -050010283 if (dytc_version >= 5) {
10284 dbg_printk(TPACPI_DBG_INIT,
10285 "DYTC version %d: thermal mode available\n", dytc_version);
Mark Pearson7a47f862021-04-06 19:32:03 -040010286 /*
10287 * Check if MMC_GET functionality available
10288 * Version > 6 and return success from MMC_GET command
10289 */
10290 dytc_mmc_get_available = false;
10291 if (dytc_version >= 6) {
10292 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10293 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10294 dytc_mmc_get_available = true;
10295 }
Mark Pearson0b4f5c92021-03-11 12:48:43 -050010296 /* Create platform_profile structure and register */
10297 err = platform_profile_register(&dytc_profile);
10298 /*
10299 * If for some reason platform_profiles aren't enabled
10300 * don't quit terminally.
10301 */
10302 if (err)
10303 return 0;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010304
Mark Pearson0b4f5c92021-03-11 12:48:43 -050010305 dytc_profile_available = true;
10306 /* Ensure initial values are correct */
10307 dytc_profile_refresh();
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010308 }
10309 return 0;
10310}
10311
10312static void dytc_profile_exit(void)
10313{
10314 if (dytc_profile_available) {
10315 dytc_profile_available = false;
10316 platform_profile_remove();
10317 }
10318}
10319
10320static struct ibm_struct dytc_profile_driver_data = {
10321 .name = "dytc-profile",
10322 .exit = dytc_profile_exit,
10323};
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010324
Nitin Joshid7cbe272021-01-25 11:59:16 +090010325/*************************************************************************
10326 * Keyboard language interface
10327 */
10328
10329struct keyboard_lang_data {
10330 const char *lang_str;
10331 int lang_code;
10332};
10333
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010334static const struct keyboard_lang_data keyboard_lang_data[] = {
Nitin Joshid7cbe272021-01-25 11:59:16 +090010335 {"be", 0x080c},
10336 {"cz", 0x0405},
10337 {"da", 0x0406},
10338 {"de", 0x0c07},
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010339 {"en", 0x0000},
Nitin Joshid7cbe272021-01-25 11:59:16 +090010340 {"es", 0x2c0a},
10341 {"et", 0x0425},
10342 {"fr", 0x040c},
10343 {"fr-ch", 0x100c},
10344 {"hu", 0x040e},
10345 {"it", 0x0410},
10346 {"jp", 0x0411},
10347 {"nl", 0x0413},
10348 {"nn", 0x0414},
10349 {"pl", 0x0415},
10350 {"pt", 0x0816},
10351 {"sl", 0x041b},
10352 {"sv", 0x081d},
10353 {"tr", 0x041f},
10354};
10355
10356static int set_keyboard_lang_command(int command)
10357{
10358 acpi_handle sskl_handle;
10359 int output;
10360
10361 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10362 /* Platform doesn't support SSKL */
10363 return -ENODEV;
10364 }
10365
10366 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10367 return -EIO;
10368
10369 return 0;
10370}
10371
10372static int get_keyboard_lang(int *output)
10373{
10374 acpi_handle gskl_handle;
10375 int kbd_lang;
10376
10377 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10378 /* Platform doesn't support GSKL */
10379 return -ENODEV;
10380 }
10381
10382 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10383 return -EIO;
10384
Hans de Goede9e9c6412021-01-25 21:52:58 +010010385 /*
10386 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10387 * '(' and ')') keys which use layout dependent key-press emulation.
10388 */
10389 if (kbd_lang & METHOD_ERR)
10390 return -ENODEV;
10391
Nitin Joshid7cbe272021-01-25 11:59:16 +090010392 *output = kbd_lang;
10393
10394 return 0;
10395}
10396
10397/* sysfs keyboard language entry */
10398static ssize_t keyboard_lang_show(struct device *dev,
10399 struct device_attribute *attr,
10400 char *buf)
10401{
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010402 int output, err, i, len = 0;
Nitin Joshid7cbe272021-01-25 11:59:16 +090010403
10404 err = get_keyboard_lang(&output);
10405 if (err)
10406 return err;
10407
10408 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10409 if (i)
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010410 len += sysfs_emit_at(buf, len, "%s", " ");
Nitin Joshid7cbe272021-01-25 11:59:16 +090010411
10412 if (output == keyboard_lang_data[i].lang_code) {
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010413 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
Nitin Joshid7cbe272021-01-25 11:59:16 +090010414 } else {
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010415 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
Nitin Joshid7cbe272021-01-25 11:59:16 +090010416 }
10417 }
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010418 len += sysfs_emit_at(buf, len, "\n");
Nitin Joshid7cbe272021-01-25 11:59:16 +090010419
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010420 return len;
Nitin Joshid7cbe272021-01-25 11:59:16 +090010421}
10422
10423static ssize_t keyboard_lang_store(struct device *dev,
10424 struct device_attribute *attr,
10425 const char *buf, size_t count)
10426{
10427 int err, i;
10428 bool lang_found = false;
10429 int lang_code = 0;
10430
10431 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10432 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10433 lang_code = keyboard_lang_data[i].lang_code;
10434 lang_found = true;
10435 break;
10436 }
10437 }
10438
10439 if (lang_found) {
10440 lang_code = lang_code | 1 << 24;
10441
10442 /* Set language code */
10443 err = set_keyboard_lang_command(lang_code);
10444 if (err)
10445 return err;
10446 } else {
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010447 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
Nitin Joshid7cbe272021-01-25 11:59:16 +090010448 return -EINVAL;
10449 }
10450
10451 tpacpi_disclose_usertask(attr->attr.name,
10452 "keyboard language is set to %s\n", buf);
10453
10454 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10455
10456 return count;
10457}
Nitin Joshid7cbe272021-01-25 11:59:16 +090010458static DEVICE_ATTR_RW(keyboard_lang);
10459
10460static struct attribute *kbdlang_attributes[] = {
10461 &dev_attr_keyboard_lang.attr,
10462 NULL
10463};
10464
10465static const struct attribute_group kbdlang_attr_group = {
10466 .attrs = kbdlang_attributes,
10467};
10468
10469static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10470{
10471 int err, output;
10472
10473 err = get_keyboard_lang(&output);
10474 /*
10475 * If support isn't available (ENODEV) then don't return an error
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010476 * just don't create the sysfs group.
Nitin Joshid7cbe272021-01-25 11:59:16 +090010477 */
10478 if (err == -ENODEV)
10479 return 0;
10480
10481 if (err)
10482 return err;
10483
10484 /* Platform supports this feature - create the sysfs file */
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010485 return sysfs_create_group(&tpacpi_pdev->dev.kobj, &kbdlang_attr_group);
Nitin Joshid7cbe272021-01-25 11:59:16 +090010486}
10487
10488static void kbdlang_exit(void)
10489{
10490 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &kbdlang_attr_group);
10491}
10492
10493static struct ibm_struct kbdlang_driver_data = {
10494 .name = "kbdlang",
10495 .exit = kbdlang_exit,
10496};
10497
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010498/*************************************************************************
10499 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10500 * and WLAN feature.
10501 */
10502#define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10503#define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10504#define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10505static bool has_antennatype;
10506static int wwan_antennatype;
10507
10508static int dprc_command(int command, int *output)
10509{
10510 acpi_handle dprc_handle;
10511
10512 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10513 /* Platform doesn't support DPRC */
10514 return -ENODEV;
10515 }
10516
10517 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10518 return -EIO;
10519
10520 /*
10521 * METHOD_ERR gets returned on devices where few commands are not supported
10522 * for example command to get WWAN Antenna type command is not supported on
10523 * some devices.
10524 */
10525 if (*output & METHOD_ERR)
10526 return -ENODEV;
10527
10528 return 0;
10529}
10530
10531static int get_wwan_antenna(int *wwan_antennatype)
10532{
10533 int output, err;
10534
10535 /* Get current Antenna type */
10536 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10537 if (err)
10538 return err;
10539
10540 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10541 *wwan_antennatype = 1;
10542 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10543 *wwan_antennatype = 2;
10544 else
10545 return -ENODEV;
10546
10547 return 0;
10548}
10549
10550/* sysfs wwan antenna type entry */
10551static ssize_t wwan_antenna_type_show(struct device *dev,
10552 struct device_attribute *attr,
10553 char *buf)
10554{
10555 switch (wwan_antennatype) {
10556 case 1:
10557 return sysfs_emit(buf, "type a\n");
10558 case 2:
10559 return sysfs_emit(buf, "type b\n");
10560 default:
10561 return -ENODATA;
10562 }
10563}
10564static DEVICE_ATTR_RO(wwan_antenna_type);
10565
10566static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10567{
10568 int wwanantenna_err, err;
10569
10570 wwanantenna_err = get_wwan_antenna(&wwan_antennatype);
10571 /*
10572 * If support isn't available (ENODEV) then quit, but don't
10573 * return an error.
10574 */
10575 if (wwanantenna_err == -ENODEV)
10576 return 0;
10577
10578 /* if there was an error return it */
10579 if (wwanantenna_err && (wwanantenna_err != -ENODEV))
10580 return wwanantenna_err;
10581 else if (!wwanantenna_err)
10582 has_antennatype = true;
10583
10584 if (has_antennatype) {
10585 err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_wwan_antenna_type.attr);
10586 if (err)
10587 return err;
10588 }
10589 return 0;
10590}
10591
10592static void dprc_exit(void)
10593{
10594 if (has_antennatype)
10595 sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_wwan_antenna_type.attr);
10596}
10597
10598static struct ibm_struct dprc_driver_data = {
10599 .name = "dprc",
10600 .exit = dprc_exit,
10601};
10602
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010603/****************************************************************************
10604 ****************************************************************************
10605 *
10606 * Infrastructure
10607 *
10608 ****************************************************************************
10609 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070010610
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010611/*
10612 * HKEY event callout for other subdrivers go here
10613 * (yes, it is ugly, but it is quick, safe, and gets the job done
10614 */
10615static void tpacpi_driver_event(const unsigned int hkey_event)
10616{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +000010617 if (ibm_backlight_device) {
10618 switch (hkey_event) {
10619 case TP_HKEY_EV_BRGHT_UP:
10620 case TP_HKEY_EV_BRGHT_DOWN:
10621 tpacpi_brightness_notify_change();
10622 }
10623 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010624 if (alsa_card) {
10625 switch (hkey_event) {
10626 case TP_HKEY_EV_VOL_UP:
10627 case TP_HKEY_EV_VOL_DOWN:
10628 case TP_HKEY_EV_VOL_MUTE:
10629 volume_alsa_notify_change();
10630 }
10631 }
Hans de Goedec685e202017-02-09 16:44:13 +010010632 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
10633 enum led_brightness brightness;
10634
10635 mutex_lock(&kbdlight_mutex);
10636
10637 /*
10638 * Check the brightness actually changed, setting the brightness
10639 * through kbdlight_set_level() also triggers this event.
10640 */
10641 brightness = kbdlight_sysfs_get(NULL);
10642 if (kbdlight_brightness != brightness) {
10643 kbdlight_brightness = brightness;
10644 led_classdev_notify_brightness_hw_changed(
10645 &tpacpi_led_kbdlight.led_classdev, brightness);
10646 }
10647
10648 mutex_unlock(&kbdlight_mutex);
10649 }
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010650
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010651 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
Mark Pearson1ac09652020-11-24 13:11:54 -050010652 lapsensor_refresh();
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010653 /* If we are already accessing DYTC then skip dytc update */
10654 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
10655 dytc_profile_refresh();
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010656 }
Hans de Goedef8098912021-10-05 22:23:20 +020010657
10658 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
10659 enum drm_privacy_screen_status old_hw_state;
10660 bool changed;
10661
10662 mutex_lock(&lcdshadow_dev->lock);
10663 old_hw_state = lcdshadow_dev->hw_state;
10664 lcdshadow_get_hw_state(lcdshadow_dev);
10665 changed = lcdshadow_dev->hw_state != old_hw_state;
10666 mutex_unlock(&lcdshadow_dev->lock);
10667
10668 if (changed)
10669 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10670 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010671}
10672
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010673static void hotkey_driver_event(const unsigned int scancode)
10674{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -030010675 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010676}
10677
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010678/* --------------------------------------------------------------------- */
10679
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010680/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -030010681static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010682
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010683/*
10684 * Module and infrastructure proble, init and exit handling
10685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010686
Rusty Russell90ab5ee2012-01-13 09:32:20 +103010687static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010688
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010689#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010690static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010691{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010692 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010693
Jan van den Berg72a979f2014-09-17 00:01:08 +020010694 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010695}
10696#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
10697
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010698static void ibm_exit(struct ibm_struct *ibm)
10699{
10700 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
10701
10702 list_del_init(&ibm->all_drivers);
10703
10704 if (ibm->flags.acpi_notify_installed) {
10705 dbg_printk(TPACPI_DBG_EXIT,
10706 "%s: acpi_remove_notify_handler\n", ibm->name);
10707 BUG_ON(!ibm->acpi);
10708 acpi_remove_notify_handler(*ibm->acpi->handle,
10709 ibm->acpi->type,
10710 dispatch_acpi_notify);
10711 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010712 }
10713
10714 if (ibm->flags.proc_created) {
10715 dbg_printk(TPACPI_DBG_EXIT,
10716 "%s: remove_proc_entry\n", ibm->name);
10717 remove_proc_entry(ibm->name, proc_dir);
10718 ibm->flags.proc_created = 0;
10719 }
10720
10721 if (ibm->flags.acpi_driver_registered) {
10722 dbg_printk(TPACPI_DBG_EXIT,
10723 "%s: acpi_bus_unregister_driver\n", ibm->name);
10724 BUG_ON(!ibm->acpi);
10725 acpi_bus_unregister_driver(ibm->acpi->driver);
10726 kfree(ibm->acpi->driver);
10727 ibm->acpi->driver = NULL;
10728 ibm->flags.acpi_driver_registered = 0;
10729 }
10730
10731 if (ibm->flags.init_called && ibm->exit) {
10732 ibm->exit();
10733 ibm->flags.init_called = 0;
10734 }
10735
10736 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
10737}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020010738
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010739static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010740{
10741 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010742 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743 struct proc_dir_entry *entry;
10744
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010745 BUG_ON(ibm == NULL);
10746
10747 INIT_LIST_HEAD(&ibm->all_drivers);
10748
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -030010749 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010750 return 0;
10751
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010752 dbg_printk(TPACPI_DBG_INIT,
10753 "probing for %s\n", ibm->name);
10754
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010755 if (iibm->init) {
10756 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010757 if (ret > 0)
10758 return 0; /* probe failed */
10759 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010760 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010761
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -030010762 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010763 }
10764
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -030010765 if (ibm->acpi) {
10766 if (ibm->acpi->hid) {
10767 ret = register_tpacpi_subdriver(ibm);
10768 if (ret)
10769 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010770 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -030010771
10772 if (ibm->acpi->notify) {
10773 ret = setup_acpi_notify(ibm);
10774 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -070010775 pr_notice("disabling subdriver %s\n",
10776 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -030010777 ret = 0;
10778 goto err_out;
10779 }
10780 if (ret < 0)
10781 goto err_out;
10782 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010783 }
10784
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010785 dbg_printk(TPACPI_DBG_INIT,
10786 "%s installed\n", ibm->name);
10787
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010788 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -040010789 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -020010790
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -030010791 if (!mode)
10792 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -020010793 if (ibm->write)
10794 mode |= S_IWUSR;
10795 entry = proc_create_data(ibm->name, mode, proc_dir,
Alexey Dobriyan97a32532020-02-03 17:37:17 -080010796 &dispatch_proc_ops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010797 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -070010798 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010799 ret = -ENODEV;
10800 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010801 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -030010802 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010804
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010805 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
10806
Linus Torvalds1da177e2005-04-16 15:20:36 -070010807 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010808
10809err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010810 dbg_printk(TPACPI_DBG_INIT,
10811 "%s: at error exit path with result %d\n",
10812 ibm->name, ret);
10813
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010814 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +020010815 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010816}
10817
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010818/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010819
Jouke Witteveen846a4162018-07-11 11:44:41 +020010820static char __init tpacpi_parse_fw_id(const char * const s,
10821 u32 *model, u16 *release)
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010822{
Jouke Witteveen846a4162018-07-11 11:44:41 +020010823 int i;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010824
Jouke Witteveen846a4162018-07-11 11:44:41 +020010825 if (!s || strlen(s) < 8)
10826 goto invalid;
10827
10828 for (i = 0; i < 8; i++)
10829 if (!((s[i] >= '0' && s[i] <= '9') ||
10830 (s[i] >= 'A' && s[i] <= 'Z')))
10831 goto invalid;
10832
Adam Lee1b0eb5b2015-02-11 13:43:10 +080010833 /*
10834 * Most models: xxyTkkWW (#.##c)
10835 * Ancient 570/600 and -SL lacks (#.##c)
10836 */
Jouke Witteveen846a4162018-07-11 11:44:41 +020010837 if (s[3] == 'T' || s[3] == 'N') {
10838 *model = TPID(s[0], s[1]);
10839 *release = TPVER(s[4], s[5]);
10840 return s[2];
Adam Lee1b0eb5b2015-02-11 13:43:10 +080010841
10842 /* New models: xxxyTkkW (#.##c); T550 and some others */
Jouke Witteveen846a4162018-07-11 11:44:41 +020010843 } else if (s[4] == 'T' || s[4] == 'N') {
10844 *model = TPID3(s[0], s[1], s[2]);
10845 *release = TPVER(s[5], s[6]);
10846 return s[3];
10847 }
10848
10849invalid:
10850 return '\0';
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010851}
10852
Jiaxun Yang9a4b33d2019-03-08 21:14:26 +080010853static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10854{
10855 char *ec_fw_string = (char *) private;
10856 const char *dmi_data = (const char *)dm;
10857 /*
10858 * ThinkPad Embedded Controller Program Table on newer models
10859 *
10860 * Offset | Name | Width | Description
10861 * ----------------------------------------------------
10862 * 0x00 | Type | BYTE | 0x8C
10863 * 0x01 | Length | BYTE |
10864 * 0x02 | Handle | WORD | Varies
10865 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
10866 * 0x0A | OEM struct offset | BYTE | 0x0B
10867 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
10868 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
10869 * 0x0D | ECP version ID | STR ID |
10870 * 0x0E | ECP release date | STR ID |
10871 */
10872
10873 /* Return if data structure not match */
10874 if (dm->type != 140 || dm->length < 0x0F ||
10875 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
10876 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
10877 dmi_data[0x0C] != 0x01)
10878 return;
10879
10880 /* fwstr is the first 8byte string */
10881 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
10882}
10883
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010884/* returns 0 - probe ok, or < 0 - probe error.
10885 * Probe ok doesn't mean thinkpad found.
10886 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
10887static int __must_check __init get_thinkpad_model_data(
10888 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020010889{
Jeff Garzik18552562007-10-03 15:15:40 -040010890 const struct dmi_device *dev = NULL;
Jiaxun Yang9a4b33d2019-03-08 21:14:26 +080010891 char ec_fw_string[18] = {0};
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010892 char const *s;
Jouke Witteveen846a4162018-07-11 11:44:41 +020010893 char t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020010894
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010895 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010896 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010897
10898 memset(tp, 0, sizeof(*tp));
10899
10900 if (dmi_name_in_vendors("IBM"))
10901 tp->vendor = PCI_VENDOR_ID_IBM;
10902 else if (dmi_name_in_vendors("LENOVO"))
10903 tp->vendor = PCI_VENDOR_ID_LENOVO;
10904 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010905 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010906
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010907 s = dmi_get_system_info(DMI_BIOS_VERSION);
10908 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
10909 if (s && !tp->bios_version_str)
10910 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010911
10912 /* Really ancient ThinkPad 240X will fail this, which is fine */
Jouke Witteveen846a4162018-07-11 11:44:41 +020010913 t = tpacpi_parse_fw_id(tp->bios_version_str,
10914 &tp->bios_model, &tp->bios_release);
10915 if (t != 'E' && t != 'C')
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010916 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010917
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020010918 /*
10919 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
10920 * X32 or newer, all Z series; Some models must have an
10921 * up-to-date BIOS or they will not be detected.
10922 *
Alexander A. Klimov19206a12020-07-13 21:06:28 +020010923 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020010924 */
10925 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020010926 if (sscanf(dev->name,
10927 "IBM ThinkPad Embedded Controller -[%17c",
10928 ec_fw_string) == 1) {
10929 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
10930 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010931 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020010932 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020010933 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010934
Jiaxun Yang9a4b33d2019-03-08 21:14:26 +080010935 /* Newer ThinkPads have different EC program info table */
10936 if (!ec_fw_string[0])
10937 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
10938
10939 if (ec_fw_string[0]) {
10940 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
10941 if (!tp->ec_version_str)
10942 return -ENOMEM;
10943
10944 t = tpacpi_parse_fw_id(ec_fw_string,
10945 &tp->ec_model, &tp->ec_release);
10946 if (t != 'H') {
10947 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
10948 ec_fw_string);
10949 pr_notice("please report this to %s\n", TPACPI_MAIL);
10950 }
10951 }
10952
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010953 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -070010954 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010955 tp->model_str = kstrdup(s, GFP_KERNEL);
10956 if (!tp->model_str)
10957 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -050010958 } else {
10959 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -070010960 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -050010961 tp->model_str = kstrdup(s, GFP_KERNEL);
10962 if (!tp->model_str)
10963 return -ENOMEM;
10964 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010965 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -030010966
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010967 s = dmi_get_system_info(DMI_PRODUCT_NAME);
10968 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
10969 if (s && !tp->nummodel_str)
10970 return -ENOMEM;
10971
10972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010973}
10974
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010975static int __init probe_for_thinkpad(void)
10976{
10977 int is_thinkpad;
10978
10979 if (acpi_disabled)
10980 return -ENODEV;
10981
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030010982 /* It would be dangerous to run the driver in this case */
10983 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
10984 return -ENODEV;
10985
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010986 /*
10987 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -030010988 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010989 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -030010990 is_thinkpad = (thinkpad_id.model_str != NULL) ||
10991 (thinkpad_id.ec_model != 0) ||
10992 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010993
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -030010994 /* The EC handler is required */
10995 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010996 if (!ec_handle) {
10997 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -070010998 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010999 return -ENODEV;
11000 }
11001
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030011002 if (!is_thinkpad && !force_load)
11003 return -ENODEV;
11004
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011005 return 0;
11006}
11007
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011008static void __init thinkpad_acpi_init_banner(void)
11009{
Joe Perches0978e012011-04-04 10:06:25 -070011010 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11011 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011012
Joe Perches0978e012011-04-04 10:06:25 -070011013 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011014 (thinkpad_id.bios_version_str) ?
11015 thinkpad_id.bios_version_str : "unknown",
11016 (thinkpad_id.ec_version_str) ?
11017 thinkpad_id.ec_version_str : "unknown");
11018
11019 BUG_ON(!thinkpad_id.vendor);
11020
11021 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -070011022 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011023 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11024 "IBM" : ((thinkpad_id.vendor ==
11025 PCI_VENDOR_ID_LENOVO) ?
11026 "Lenovo" : "Unknown vendor"),
11027 thinkpad_id.model_str,
11028 (thinkpad_id.nummodel_str) ?
11029 thinkpad_id.nummodel_str : "unknown");
11030}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011031
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011032/* Module init, exit, parameters */
11033
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011034static struct ibm_init_struct ibms_init[] __initdata = {
11035 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011036 .data = &thinkpad_acpi_driver_data,
11037 },
11038 {
11039 .init = hotkey_init,
11040 .data = &hotkey_driver_data,
11041 },
11042 {
11043 .init = bluetooth_init,
11044 .data = &bluetooth_driver_data,
11045 },
11046 {
11047 .init = wan_init,
11048 .data = &wan_driver_data,
11049 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020011050 {
11051 .init = uwb_init,
11052 .data = &uwb_driver_data,
11053 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020011054#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011055 {
11056 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -030011057 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011058 .data = &video_driver_data,
11059 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020011060#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011061 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +010011062 .init = kbdlight_init,
11063 .data = &kbdlight_driver_data,
11064 },
11065 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011066 .init = light_init,
11067 .data = &light_driver_data,
11068 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011069 {
11070 .init = cmos_init,
11071 .data = &cmos_driver_data,
11072 },
11073 {
11074 .init = led_init,
11075 .data = &led_driver_data,
11076 },
11077 {
11078 .init = beep_init,
11079 .data = &beep_driver_data,
11080 },
11081 {
11082 .init = thermal_init,
11083 .data = &thermal_driver_data,
11084 },
11085 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011086 .init = brightness_init,
11087 .data = &brightness_driver_data,
11088 },
11089 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020011090 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011091 .data = &volume_driver_data,
11092 },
11093 {
11094 .init = fan_init,
11095 .data = &fan_driver_data,
11096 },
David Henningsson420f9732013-10-16 23:10:31 +020011097 {
11098 .init = mute_led_init,
11099 .data = &mute_led_driver_data,
11100 },
Ognjen Galic2801b962018-02-07 15:58:44 +010011101 {
11102 .init = tpacpi_battery_init,
11103 .data = &battery_driver_data,
11104 },
Alexander Schremmer110ea1d2019-08-22 13:48:33 +020011105 {
11106 .init = tpacpi_lcdshadow_init,
11107 .data = &lcdshadow_driver_data,
11108 },
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040011109 {
Mark Pearson1ac09652020-11-24 13:11:54 -050011110 .init = tpacpi_proxsensor_init,
11111 .data = &proxsensor_driver_data,
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040011112 },
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050011113 {
11114 .init = tpacpi_dytc_profile_init,
11115 .data = &dytc_profile_driver_data,
11116 },
Nitin Joshid7cbe272021-01-25 11:59:16 +090011117 {
11118 .init = tpacpi_kbdlang_init,
11119 .data = &kbdlang_driver_data,
11120 },
Nitin Joshi3feb52a2021-03-17 11:46:36 +090011121 {
11122 .init = tpacpi_dprc_init,
11123 .data = &dprc_driver_data,
11124 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011125};
11126
Kees Cooke4dca7b2017-10-17 19:04:42 -070011127static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011128{
11129 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011130 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011131
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020011132 if (!kp || !kp->name || !val)
11133 return -EINVAL;
11134
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011135 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11136 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020011137 WARN_ON(ibm == NULL);
11138
11139 if (!ibm || !ibm->name)
11140 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011141
11142 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
Andy Shevchenkobe51bd42020-05-11 15:56:24 +030011143 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011144 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011145 strcpy(ibms_init[i].param, val);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011146 return 0;
11147 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011148 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011149
11150 return -EINVAL;
11151}
11152
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011153module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011154MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011155 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011156
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030011157module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011158MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030011159
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011160module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011161MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011162 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030011163
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011164module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011165MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011166 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -030011167
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011168module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011169MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011170 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -030011171
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011172module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011173MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011174 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -020011175
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020011176#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020011177module_param_named(volume_mode, volume_mode, uint, 0444);
11178MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011179 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020011180
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020011181module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11182MODULE_PARM_DESC(volume_capabilities,
Colin Ian Kingd33a7e52019-04-15 13:52:04 +010011183 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020011184
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020011185module_param_named(volume_control, volume_control_allowed, bool, 0444);
11186MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011187 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020011188
Andy Lutomirski9a417ec2014-10-17 17:04:29 -070011189module_param_named(software_mute, software_mute_requested, bool, 0444);
11190MODULE_PARM_DESC(software_mute,
11191 "Request full software mute control");
11192
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020011193/* ALSA module API parameters */
11194module_param_named(index, alsa_index, int, 0444);
11195MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11196module_param_named(id, alsa_id, charp, 0444);
11197MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11198module_param_named(enable, alsa_enable, bool, 0444);
11199MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020011200#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020011201
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +030011202/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011203#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011204 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011205 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011206
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011207TPACPI_PARAM(hotkey);
11208TPACPI_PARAM(bluetooth);
11209TPACPI_PARAM(video);
11210TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011211TPACPI_PARAM(cmos);
11212TPACPI_PARAM(led);
11213TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011214TPACPI_PARAM(brightness);
11215TPACPI_PARAM(volume);
11216TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011217
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011218#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011219module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011220MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11221module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11222MODULE_PARM_DESC(wlsw_state,
11223 "Initial state of the emulated WLSW switch");
11224
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011225module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011226MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11227module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11228MODULE_PARM_DESC(bluetooth_state,
11229 "Initial state of the emulated bluetooth switch");
11230
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011231module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011232MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11233module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11234MODULE_PARM_DESC(wwan_state,
11235 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020011236
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011237module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020011238MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11239module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11240MODULE_PARM_DESC(uwb_state,
11241 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011242#endif
11243
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011244static void thinkpad_acpi_module_exit(void)
11245{
11246 struct ibm_struct *ibm, *itmp;
11247
11248 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11249
11250 list_for_each_entry_safe_reverse(ibm, itmp,
11251 &tpacpi_all_drivers,
11252 all_drivers) {
11253 ibm_exit(ibm);
11254 }
11255
11256 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11257
11258 if (tpacpi_inputdev) {
11259 if (tp_features.input_device_registered)
11260 input_unregister_device(tpacpi_inputdev);
11261 else
11262 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +100011263 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011264 }
11265
11266 if (tpacpi_hwmon)
11267 hwmon_device_unregister(tpacpi_hwmon);
11268
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011269 if (tpacpi_sensors_pdev)
11270 platform_device_unregister(tpacpi_sensors_pdev);
11271 if (tpacpi_pdev)
11272 platform_device_unregister(tpacpi_pdev);
11273
11274 if (tp_features.sensors_pdrv_attrs_registered)
11275 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
11276 if (tp_features.platform_drv_attrs_registered)
11277 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
11278
11279 if (tp_features.sensors_pdrv_registered)
11280 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11281
11282 if (tp_features.platform_drv_registered)
11283 platform_driver_unregister(&tpacpi_pdriver);
11284
11285 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011286 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011287
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030011288 if (tpacpi_wq)
11289 destroy_workqueue(tpacpi_wq);
11290
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011291 kfree(thinkpad_id.bios_version_str);
11292 kfree(thinkpad_id.ec_version_str);
11293 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +100011294 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011295}
11296
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020011297
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011298static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011299{
11300 int ret, i;
11301
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -030011302 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11303
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011304 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011305
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011306 ret = get_thinkpad_model_data(&thinkpad_id);
11307 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070011308 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011309 thinkpad_acpi_module_exit();
11310 return ret;
11311 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011312 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011313 if (ret) {
11314 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011315 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011317
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011318 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011319
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011320 thinkpad_acpi_init_banner();
11321 tpacpi_check_outdated_fw();
11322
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011323 TPACPI_ACPIHANDLE_INIT(ecrd);
11324 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011325
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030011326 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11327 if (!tpacpi_wq) {
11328 thinkpad_acpi_module_exit();
11329 return -ENOMEM;
11330 }
11331
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011332 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011333 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -070011334 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011335 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070011336 return -ENODEV;
11337 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -040011338
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011339 ret = platform_driver_register(&tpacpi_pdriver);
11340 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070011341 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011342 thinkpad_acpi_module_exit();
11343 return ret;
11344 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -030011345 tp_features.platform_drv_registered = 1;
11346
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011347 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11348 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070011349 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011350 thinkpad_acpi_module_exit();
11351 return ret;
11352 }
11353 tp_features.sensors_pdrv_registered = 1;
11354
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030011355 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030011356 if (!ret) {
11357 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011358 ret = tpacpi_create_driver_attributes(
11359 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030011360 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030011361 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070011362 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030011363 thinkpad_acpi_module_exit();
11364 return ret;
11365 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030011366 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030011367
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011368
11369 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011370 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011371 NULL, 0);
11372 if (IS_ERR(tpacpi_pdev)) {
11373 ret = PTR_ERR(tpacpi_pdev);
11374 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070011375 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011376 thinkpad_acpi_module_exit();
11377 return ret;
11378 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011379 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011380 TPACPI_HWMON_DRVR_NAME,
11381 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011382 if (IS_ERR(tpacpi_sensors_pdev)) {
11383 ret = PTR_ERR(tpacpi_sensors_pdev);
11384 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070011385 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011386 thinkpad_acpi_module_exit();
11387 return ret;
11388 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011389 tp_features.sensors_pdev_attrs_registered = 1;
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070011390 tpacpi_hwmon = hwmon_device_register_with_groups(
11391 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
11392
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011393 if (IS_ERR(tpacpi_hwmon)) {
11394 ret = PTR_ERR(tpacpi_hwmon);
11395 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070011396 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011397 thinkpad_acpi_module_exit();
11398 return ret;
11399 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -030011400 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011401 tpacpi_inputdev = input_allocate_device();
11402 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011403 thinkpad_acpi_module_exit();
11404 return -ENOMEM;
11405 } else {
11406 /* Prepare input device, but don't register */
11407 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011408 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011409 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030011410 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011411 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11412 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +000011413 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011414 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -030011415
11416 /* Init subdriver dependencies */
11417 tpacpi_detect_brightness_capabilities();
11418
11419 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011420 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11421 ret = ibm_init(&ibms_init[i]);
11422 if (ret >= 0 && *ibms_init[i].param)
11423 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011424 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011425 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070011426 return ret;
11427 }
11428 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -030011429
11430 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11431
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011432 ret = input_register_device(tpacpi_inputdev);
11433 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -070011434 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011435 thinkpad_acpi_module_exit();
11436 return ret;
11437 } else {
11438 tp_features.input_device_registered = 1;
11439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011440
11441 return 0;
11442}
11443
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -030011444MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11445
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011446/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000011447 * This will autoload the driver in almost every ThinkPad
11448 * in widespread use.
11449 *
11450 * Only _VERY_ old models, like the 240, 240x and 570 lack
11451 * the HKEY event interface.
11452 */
11453MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
11454
11455/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011456 * DMI matching for module autoloading
11457 *
Alexander A. Klimov19206a12020-07-13 21:06:28 +020011458 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11459 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011460 *
11461 * Only models listed in thinkwiki will be supported, so add yours
11462 * if it is not there yet.
11463 */
11464#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +010011465 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011466
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011467/* Ancient thinkpad BIOSes have to be identified by
11468 * BIOS type or model number, and there are far less
11469 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000011470IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011471
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +000011472MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
11473MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011474MODULE_DESCRIPTION(TPACPI_DESC);
11475MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011476MODULE_LICENSE("GPL");
11477
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011478module_init(thinkpad_acpi_module_init);
11479module_exit(thinkpad_acpi_module_exit);