blob: 098180fb1cfc9c0c34837006628a872e609e07b8 [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;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200337 u32 sensors_pdrv_registered:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200338 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100339 u32 has_adaptive_kbd:1;
Len Baker79f960e2021-10-23 17:40:36 +0200340 u32 kbd_lang:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200341} tp_features;
342
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300343static struct {
344 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200345 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300346} tp_warned;
347
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200348struct thinkpad_id_data {
349 unsigned int vendor; /* ThinkPad vendor:
350 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
351
352 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
353 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
354
Jouke Witteveen846a4162018-07-11 11:44:41 +0200355 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
356 u32 ec_model;
357 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300358 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200359
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300360 char *model_str; /* ThinkPad T43 */
361 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200362};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200363static struct thinkpad_id_data thinkpad_id;
364
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300365static enum {
366 TPACPI_LIFE_INIT = 0,
367 TPACPI_LIFE_RUNNING,
368 TPACPI_LIFE_EXITING,
369} tpacpi_lifecycle;
370
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200371static int experimental;
372static u32 dbg_level;
373
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300374static struct workqueue_struct *tpacpi_wq;
375
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000376enum led_status_t {
377 TPACPI_LED_OFF = 0,
378 TPACPI_LED_ON,
379 TPACPI_LED_BLINK,
380};
381
Hans de Goede86ec0c22017-02-09 16:44:12 +0100382/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300383struct tpacpi_led_classdev {
384 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800385 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300386};
387
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300388/* brightness level capabilities */
389static unsigned int bright_maxlvl; /* 0 = unknown */
390
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200391#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
392static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030393static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200394static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030395static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200396static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030397static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200398static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030399static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200400#endif
401
402
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000403/*************************************************************************
404 * Debugging helpers
405 */
406
Joe Perches0978e012011-04-04 10:06:25 -0700407#define dbg_printk(a_dbg_level, format, arg...) \
408do { \
409 if (dbg_level & (a_dbg_level)) \
410 printk(KERN_DEBUG pr_fmt("%s: " format), \
411 __func__, ##arg); \
412} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000413
414#ifdef CONFIG_THINKPAD_ACPI_DEBUG
415#define vdbg_printk dbg_printk
416static const char *str_supported(int is_supported);
417#else
Joe Perches0978e012011-04-04 10:06:25 -0700418static inline const char *str_supported(int is_supported) { return ""; }
419#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700420 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000421#endif
422
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000423static void tpacpi_log_usertask(const char * const what)
424{
Joe Perches0978e012011-04-04 10:06:25 -0700425 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
426 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000427}
428
Joe Perches0978e012011-04-04 10:06:25 -0700429#define tpacpi_disclose_usertask(what, format, arg...) \
430do { \
431 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
432 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
433 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
434 what, task_tgid_vnr(current), ## arg); \
435 } \
436} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000437
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300438/*
439 * Quirk handling helpers
440 *
Jouke Witteveen846a4162018-07-11 11:44:41 +0200441 * ThinkPad IDs and versions seen in the field so far are
442 * two or three characters from the set [0-9A-Z], i.e. base 36.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300443 *
444 * We use values well outside that range as specials.
445 */
446
Jouke Witteveen846a4162018-07-11 11:44:41 +0200447#define TPACPI_MATCH_ANY 0xffffffffU
448#define TPACPI_MATCH_ANY_VERSION 0xffffU
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300449#define TPACPI_MATCH_UNKNOWN 0U
450
Jouke Witteveen846a4162018-07-11 11:44:41 +0200451/* TPID('1', 'Y') == 0x3159 */
452#define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
453#define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
454#define TPVER TPID
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300455
456#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
457 { .vendor = PCI_VENDOR_ID_IBM, \
458 .bios = TPID(__id1, __id2), \
459 .ec = TPACPI_MATCH_ANY, \
460 .quirks = (__quirk) }
461
462#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
463 { .vendor = PCI_VENDOR_ID_LENOVO, \
464 .bios = TPID(__id1, __id2), \
465 .ec = TPACPI_MATCH_ANY, \
466 .quirks = (__quirk) }
467
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +0200468#define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
469 { .vendor = PCI_VENDOR_ID_LENOVO, \
470 .bios = TPID3(__id1, __id2, __id3), \
471 .ec = TPACPI_MATCH_ANY, \
472 .quirks = (__quirk) }
473
Jouke Witteveen599eeff2018-12-05 12:17:52 +0100474#define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
475 { .vendor = PCI_VENDOR_ID_IBM, \
476 .bios = TPACPI_MATCH_ANY, \
477 .ec = TPID(__id1, __id2), \
478 .quirks = (__quirk) }
479
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200480#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
481 { .vendor = PCI_VENDOR_ID_LENOVO, \
482 .bios = TPACPI_MATCH_ANY, \
483 .ec = TPID(__id1, __id2), \
484 .quirks = (__quirk) }
485
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300486struct tpacpi_quirk {
487 unsigned int vendor;
Jouke Witteveen846a4162018-07-11 11:44:41 +0200488 u32 bios;
489 u32 ec;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300490 unsigned long quirks;
491};
492
493/**
494 * tpacpi_check_quirks() - search BIOS/EC version on a list
495 * @qlist: array of &struct tpacpi_quirk
496 * @qlist_size: number of elements in @qlist
497 *
498 * Iterates over a quirks list until one is found that matches the
499 * ThinkPad's vendor, BIOS and EC model.
500 *
501 * Returns 0 if nothing matches, otherwise returns the quirks field of
502 * the matching &struct tpacpi_quirk entry.
503 *
504 * The match criteria is: vendor, ec and bios much match.
505 */
506static unsigned long __init tpacpi_check_quirks(
507 const struct tpacpi_quirk *qlist,
508 unsigned int qlist_size)
509{
510 while (qlist_size) {
511 if ((qlist->vendor == thinkpad_id.vendor ||
512 qlist->vendor == TPACPI_MATCH_ANY) &&
513 (qlist->bios == thinkpad_id.bios_model ||
514 qlist->bios == TPACPI_MATCH_ANY) &&
515 (qlist->ec == thinkpad_id.ec_model ||
516 qlist->ec == TPACPI_MATCH_ANY))
517 return qlist->quirks;
518
519 qlist_size--;
520 qlist++;
521 }
522 return 0;
523}
524
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300525static inline bool __pure __init tpacpi_is_lenovo(void)
526{
527 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
528}
529
530static inline bool __pure __init tpacpi_is_ibm(void)
531{
532 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
533}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300534
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535/****************************************************************************
536 ****************************************************************************
537 *
538 * ACPI Helpers and device model
539 *
540 ****************************************************************************
541 ****************************************************************************/
542
543/*************************************************************************
544 * ACPI basic handles
545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300547static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300548static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200550#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700552 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300553 &parent##_handle; \
554 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200556TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
557TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200559TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
560 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400561 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
562 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400564
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200565TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400566 "^HKEY", /* R30, R31 */
567 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300568 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400569
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570/*************************************************************************
571 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200572 */
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700575 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400578 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200579 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400580 struct acpi_buffer result, *resultp;
581 union acpi_object out_obj;
582 acpi_status status;
583 va_list ap;
584 char res_type;
585 int success;
586 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700589 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591 }
592
593 if (*fmt == 'q') {
594 quiet = 1;
595 fmt++;
596 } else
597 quiet = 0;
598
599 res_type = *(fmt++);
600
601 params.count = 0;
602 params.pointer = &in_objs[0];
603
604 va_start(ap, fmt);
605 while (*fmt) {
606 char c = *(fmt++);
607 switch (c) {
608 case 'd': /* int */
609 in_objs[params.count].integer.value = va_arg(ap, int);
610 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
611 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400612 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300614 pr_err("acpi_evalf() called with invalid format character '%c'\n",
615 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100616 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 0;
618 }
619 }
620 va_end(ap);
621
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400622 if (res_type != 'v') {
623 result.length = sizeof(out_obj);
624 result.pointer = &out_obj;
625 resultp = &result;
626 } else
627 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400629 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400632 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300633 success = (status == AE_OK &&
634 out_obj.type == ACPI_TYPE_INTEGER);
635 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700636 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400638 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 success = status == AE_OK;
640 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400641 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300643 pr_err("acpi_evalf() called with invalid format character '%c'\n",
644 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646 }
647
648 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700649 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300650 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 return success;
653}
654
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200655static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300656{
657 int v;
658
659 if (ecrd_handle) {
660 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
661 return 0;
662 *p = v;
663 } else {
664 if (ec_read(i, p) < 0)
665 return 0;
666 }
667
668 return 1;
669}
670
671static int acpi_ec_write(int i, u8 v)
672{
673 if (ecwr_handle) {
674 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
675 return 0;
676 } else {
677 if (ec_write(i, v) < 0)
678 return 0;
679 }
680
681 return 1;
682}
683
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300684static int issue_thinkpad_cmos_command(int cmos_cmd)
685{
686 if (!cmos_handle)
687 return -ENXIO;
688
689 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
690 return -EIO;
691
692 return 0;
693}
694
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695/*************************************************************************
696 * ACPI device model
697 */
698
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200699#define TPACPI_ACPIHANDLE_INIT(object) \
700 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300701 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200702
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300703static void __init drv_acpi_handle_init(const char *name,
704 acpi_handle *handle, const acpi_handle parent,
705 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300706{
707 int i;
708 acpi_status status;
709
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300710 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
711 name);
712
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300713 for (i = 0; i < num_paths; i++) {
714 status = acpi_get_handle(parent, paths[i], handle);
715 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300716 dbg_printk(TPACPI_DBG_INIT,
717 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300718 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300719 return;
720 }
721 }
722
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300723 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
724 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725 *handle = NULL;
726}
727
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300728static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
729 u32 level, void *context, void **return_value)
730{
Aaron Lu46445b62013-10-11 21:27:46 +0800731 struct acpi_device *dev;
732 if (!strcmp(context, "video")) {
733 if (acpi_bus_get_device(handle, &dev))
734 return AE_OK;
735 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
736 return AE_OK;
737 }
738
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300739 *(acpi_handle *)return_value = handle;
740
741 return AE_CTRL_TERMINATE;
742}
743
744static void __init tpacpi_acpi_handle_locate(const char *name,
745 const char *hid,
746 acpi_handle *handle)
747{
748 acpi_status status;
749 acpi_handle device_found;
750
Aaron Lu46445b62013-10-11 21:27:46 +0800751 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300752 vdbg_printk(TPACPI_DBG_INIT,
753 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800754 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300755
756 memset(&device_found, 0, sizeof(device_found));
757 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
758 (void *)name, &device_found);
759
760 *handle = NULL;
761
762 if (ACPI_SUCCESS(status)) {
763 *handle = device_found;
764 dbg_printk(TPACPI_DBG_INIT,
765 "Found ACPI handle for %s\n", name);
766 } else {
767 vdbg_printk(TPACPI_DBG_INIT,
768 "Could not locate an ACPI handle for %s: %s\n",
769 name, acpi_format_exception(status));
770 }
771}
772
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300773static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300774{
775 struct ibm_struct *ibm = data;
776
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300777 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
778 return;
779
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300780 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 return;
782
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300783 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300784}
785
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300786static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787{
788 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300789 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300791 BUG_ON(!ibm->acpi);
792
793 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794 return 0;
795
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300796 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300797 "setting up ACPI notify for %s\n", ibm->name);
798
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300799 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
800 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700801 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300802 return -ENODEV;
803 }
804
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700805 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300806 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200807 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300808 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300809
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300810 status = acpi_install_notify_handler(*ibm->acpi->handle,
811 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300812 if (ACPI_FAILURE(status)) {
813 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300814 pr_notice("another device driver is already handling %s events\n",
815 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300816 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700817 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300818 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300819 }
820 return -ENODEV;
821 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300822 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300823 return 0;
824}
825
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300826static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300827{
828 return 0;
829}
830
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300831static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300832{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300833 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300834
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300835 dbg_printk(TPACPI_DBG_INIT,
836 "registering %s as an ACPI driver\n", ibm->name);
837
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300838 BUG_ON(!ibm->acpi);
839
840 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
841 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700842 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300843 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844 }
845
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200846 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300847 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200848
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300849 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300850
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300851 rc = acpi_bus_register_driver(ibm->acpi->driver);
852 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700853 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200854 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300855 kfree(ibm->acpi->driver);
856 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300857 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300858 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300859
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300860 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861}
862
863
864/****************************************************************************
865 ****************************************************************************
866 *
867 * Procfs Helpers
868 *
869 ****************************************************************************
870 ****************************************************************************/
871
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200872static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300873{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200874 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300875
876 if (!ibm || !ibm->read)
877 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200878 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300879}
880
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200881static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300882{
Muchun Song359745d2022-01-21 22:14:23 -0800883 return single_open(file, dispatch_proc_show, pde_data(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200884}
885
886static ssize_t dispatch_proc_write(struct file *file,
887 const char __user *userbuf,
888 size_t count, loff_t *pos)
889{
Muchun Song359745d2022-01-21 22:14:23 -0800890 struct ibm_struct *ibm = pde_data(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300891 char *kernbuf;
892 int ret;
893
894 if (!ibm || !ibm->write)
895 return -EINVAL;
Andy Shevchenkodf11f6c2020-07-15 12:40:07 +0300896 if (count > PAGE_SIZE - 1)
897 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300898
Andy Shevchenkodf11f6c2020-07-15 12:40:07 +0300899 kernbuf = kmalloc(count + 1, GFP_KERNEL);
900 if (!kernbuf)
901 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300902
Andy Shevchenkodf11f6c2020-07-15 12:40:07 +0300903 if (copy_from_user(kernbuf, userbuf, count)) {
904 kfree(kernbuf);
905 return -EFAULT;
906 }
907
908 kernbuf[count] = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300909 ret = ibm->write(kernbuf);
910 if (ret == 0)
911 ret = count;
912
913 kfree(kernbuf);
914
915 return ret;
916}
917
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800918static const struct proc_ops dispatch_proc_ops = {
919 .proc_open = dispatch_proc_open,
920 .proc_read = seq_read,
921 .proc_lseek = seq_lseek,
922 .proc_release = single_release,
923 .proc_write = dispatch_proc_write,
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200924};
925
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300926/****************************************************************************
927 ****************************************************************************
928 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300929 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300930 *
931 ****************************************************************************
932 ****************************************************************************/
933
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300934static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300935static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700936static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300937static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300938static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200939static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300940
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200941#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200942static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200943{
944 struct ibm_struct *ibm, *itmp;
945
946 list_for_each_entry_safe(ibm, itmp,
947 &tpacpi_all_drivers,
948 all_drivers) {
949 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200950 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200951 }
952
953 return 0;
954}
955
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200956static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300957{
958 struct ibm_struct *ibm, *itmp;
959
960 list_for_each_entry_safe(ibm, itmp,
961 &tpacpi_all_drivers,
962 all_drivers) {
963 if (ibm->resume)
964 (ibm->resume)();
965 }
966
967 return 0;
968}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200969#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300970
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200971static SIMPLE_DEV_PM_OPS(tpacpi_pm,
972 tpacpi_suspend_handler, tpacpi_resume_handler);
973
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200974static void tpacpi_shutdown_handler(struct platform_device *pdev)
975{
976 struct ibm_struct *ibm, *itmp;
977
978 list_for_each_entry_safe(ibm, itmp,
979 &tpacpi_all_drivers,
980 all_drivers) {
981 if (ibm->shutdown)
982 (ibm->shutdown)();
983 }
984}
985
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300986/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300987 * sysfs support helpers
988 */
989
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300990static int parse_strtoul(const char *buf,
991 unsigned long max, unsigned long *value)
992{
993 char *endp;
994
André Goddard Rosae7d28602009-12-14 18:01:06 -0800995 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
996 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300997 if (*endp || *value > max)
998 return -EINVAL;
999
1000 return 0;
1001}
1002
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001003static void tpacpi_disable_brightness_delay(void)
1004{
1005 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001006 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001007}
1008
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001009static void printk_deprecated_attribute(const char * const what,
1010 const char * const details)
1011{
1012 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001013 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001014 what, details);
1015}
1016
Johannes Berg19d337d2009-06-02 13:01:37 +02001017/*************************************************************************
1018 * rfkill and radio control support helpers
1019 */
1020
1021/*
1022 * ThinkPad-ACPI firmware handling model:
1023 *
1024 * WLSW (master wireless switch) is event-driven, and is common to all
1025 * firmware-controlled radios. It cannot be controlled, just monitored,
1026 * as expected. It overrides all radio state in firmware
1027 *
1028 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1029 * (TODO: verify how WLSW interacts with the returned radio state).
1030 *
1031 * The only time there are shadow radio state changes, is when
1032 * masked-off hotkeys are used.
1033 */
1034
1035/*
1036 * Internal driver API for radio state:
1037 *
1038 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1039 * bool: true means radio blocked (off)
1040 */
1041enum tpacpi_rfkill_state {
1042 TPACPI_RFK_RADIO_OFF = 0,
1043 TPACPI_RFK_RADIO_ON
1044};
1045
1046/* rfkill switches */
1047enum tpacpi_rfk_id {
1048 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1049 TPACPI_RFK_WWAN_SW_ID,
1050 TPACPI_RFK_UWB_SW_ID,
1051 TPACPI_RFK_SW_MAX
1052};
1053
1054static const char *tpacpi_rfkill_names[] = {
1055 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1056 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1057 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1058 [TPACPI_RFK_SW_MAX] = NULL
1059};
1060
1061/* ThinkPad-ACPI rfkill subdriver */
1062struct tpacpi_rfk {
1063 struct rfkill *rfkill;
1064 enum tpacpi_rfk_id id;
1065 const struct tpacpi_rfk_ops *ops;
1066};
1067
1068struct tpacpi_rfk_ops {
1069 /* firmware interface */
1070 int (*get_status)(void);
1071 int (*set_status)(const enum tpacpi_rfkill_state);
1072};
1073
1074static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1075
1076/* Query FW and update rfkill sw state for a given rfkill switch */
1077static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1078{
1079 int status;
1080
1081 if (!tp_rfk)
1082 return -ENODEV;
1083
1084 status = (tp_rfk->ops->get_status)();
1085 if (status < 0)
1086 return status;
1087
1088 rfkill_set_sw_state(tp_rfk->rfkill,
1089 (status == TPACPI_RFK_RADIO_OFF));
1090
1091 return status;
1092}
1093
Johannes Berg19d337d2009-06-02 13:01:37 +02001094/*
1095 * Sync the HW-blocking state of all rfkill switches,
1096 * do notice it causes the rfkill core to schedule uevents
1097 */
1098static void tpacpi_rfk_update_hwblock_state(bool blocked)
1099{
1100 unsigned int i;
1101 struct tpacpi_rfk *tp_rfk;
1102
1103 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1104 tp_rfk = tpacpi_rfkill_switches[i];
1105 if (tp_rfk) {
1106 if (rfkill_set_hw_state(tp_rfk->rfkill,
1107 blocked)) {
1108 /* ignore -- we track sw block */
1109 }
1110 }
1111 }
1112}
1113
1114/* Call to get the WLSW state from the firmware */
1115static int hotkey_get_wlsw(void);
1116
1117/* Call to query WLSW state and update all rfkill switches */
1118static bool tpacpi_rfk_check_hwblock_state(void)
1119{
1120 int res = hotkey_get_wlsw();
1121 int hw_blocked;
1122
1123 /* When unknown or unsupported, we have to assume it is unblocked */
1124 if (res < 0)
1125 return false;
1126
1127 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1128 tpacpi_rfk_update_hwblock_state(hw_blocked);
1129
1130 return hw_blocked;
1131}
1132
1133static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1134{
1135 struct tpacpi_rfk *tp_rfk = data;
1136 int res;
1137
1138 dbg_printk(TPACPI_DBG_RFKILL,
1139 "request to change radio state to %s\n",
1140 blocked ? "blocked" : "unblocked");
1141
1142 /* try to set radio state */
1143 res = (tp_rfk->ops->set_status)(blocked ?
1144 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1145
1146 /* and update the rfkill core with whatever the FW really did */
1147 tpacpi_rfk_update_swstate(tp_rfk);
1148
1149 return (res < 0) ? res : 0;
1150}
1151
1152static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1153 .set_block = tpacpi_rfk_hook_set_block,
1154};
1155
1156static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1157 const struct tpacpi_rfk_ops *tp_rfkops,
1158 const enum rfkill_type rfktype,
1159 const char *name,
1160 const bool set_default)
1161{
1162 struct tpacpi_rfk *atp_rfk;
1163 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001164 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001165 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001166 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001167
1168 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1169
Johannes Berg19d337d2009-06-02 13:01:37 +02001170 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1171 if (atp_rfk)
1172 atp_rfk->rfkill = rfkill_alloc(name,
1173 &tpacpi_pdev->dev,
1174 rfktype,
1175 &tpacpi_rfk_rfkill_ops,
1176 atp_rfk);
1177 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001178 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001179 kfree(atp_rfk);
1180 return -ENOMEM;
1181 }
1182
1183 atp_rfk->id = id;
1184 atp_rfk->ops = tp_rfkops;
1185
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001186 sw_status = (tp_rfkops->get_status)();
1187 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001188 pr_err("failed to read initial state for %s, error %d\n",
1189 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001190 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001191 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001192 if (set_default) {
1193 /* try to keep the initial state, since we ask the
1194 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001195 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001196 }
1197 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001198 hw_state = tpacpi_rfk_check_hwblock_state();
1199 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001200
1201 res = rfkill_register(atp_rfk->rfkill);
1202 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001203 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001204 rfkill_destroy(atp_rfk->rfkill);
1205 kfree(atp_rfk);
1206 return res;
1207 }
1208
1209 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001210
Joe Perches0978e012011-04-04 10:06:25 -07001211 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001212 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001213 return 0;
1214}
1215
1216static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1217{
1218 struct tpacpi_rfk *tp_rfk;
1219
1220 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1221
1222 tp_rfk = tpacpi_rfkill_switches[id];
1223 if (tp_rfk) {
1224 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001225 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001226 tpacpi_rfkill_switches[id] = NULL;
1227 kfree(tp_rfk);
1228 }
1229}
1230
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001231static void printk_deprecated_rfkill_attribute(const char * const what)
1232{
1233 printk_deprecated_attribute(what,
1234 "Please switch to generic rfkill before year 2010");
1235}
1236
Johannes Berg19d337d2009-06-02 13:01:37 +02001237/* sysfs <radio> enable ------------------------------------------------ */
1238static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1239 struct device_attribute *attr,
1240 char *buf)
1241{
1242 int status;
1243
1244 printk_deprecated_rfkill_attribute(attr->attr.name);
1245
1246 /* This is in the ABI... */
1247 if (tpacpi_rfk_check_hwblock_state()) {
1248 status = TPACPI_RFK_RADIO_OFF;
1249 } else {
1250 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1251 if (status < 0)
1252 return status;
1253 }
1254
Ye Guojin33ce79b2021-10-18 09:17:50 +00001255 return sysfs_emit(buf, "%d\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001256 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1257}
1258
1259static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1260 struct device_attribute *attr,
1261 const char *buf, size_t count)
1262{
1263 unsigned long t;
1264 int res;
1265
1266 printk_deprecated_rfkill_attribute(attr->attr.name);
1267
1268 if (parse_strtoul(buf, 1, &t))
1269 return -EINVAL;
1270
1271 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1272
1273 /* This is in the ABI... */
1274 if (tpacpi_rfk_check_hwblock_state() && !!t)
1275 return -EPERM;
1276
1277 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1278 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1279 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1280
1281 return (res < 0) ? res : count;
1282}
1283
1284/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001285static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001286{
Johannes Berg19d337d2009-06-02 13:01:37 +02001287 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001288 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001289 else {
1290 int status;
1291
1292 /* This is in the ABI... */
1293 if (tpacpi_rfk_check_hwblock_state()) {
1294 status = TPACPI_RFK_RADIO_OFF;
1295 } else {
1296 status = tpacpi_rfk_update_swstate(
1297 tpacpi_rfkill_switches[id]);
1298 if (status < 0)
1299 return status;
1300 }
1301
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001302 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001303 (status == TPACPI_RFK_RADIO_ON) ?
1304 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001305 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001306 }
1307
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001308 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001309}
1310
1311static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1312{
1313 char *cmd;
1314 int status = -1;
1315 int res = 0;
1316
1317 if (id >= TPACPI_RFK_SW_MAX)
1318 return -ENODEV;
1319
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03001320 while ((cmd = strsep(&buf, ","))) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001321 if (strlencmp(cmd, "enable") == 0)
1322 status = TPACPI_RFK_RADIO_ON;
1323 else if (strlencmp(cmd, "disable") == 0)
1324 status = TPACPI_RFK_RADIO_OFF;
1325 else
1326 return -EINVAL;
1327 }
1328
1329 if (status != -1) {
1330 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1331 (status == TPACPI_RFK_RADIO_ON) ?
1332 "enable" : "disable",
1333 tpacpi_rfkill_names[id]);
1334 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1335 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1336 }
1337
1338 return res;
1339}
1340
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001341/*************************************************************************
1342 * thinkpad-acpi driver attributes
1343 */
1344
1345/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001346static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001347{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001348 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001349}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001350static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001351
1352/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001353static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001354{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001355 return sysfs_emit(buf, "0x%04x\n", dbg_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001356}
1357
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001358static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1359 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001360{
1361 unsigned long t;
1362
1363 if (parse_strtoul(buf, 0xffff, &t))
1364 return -EINVAL;
1365
1366 dbg_level = t;
1367
1368 return count;
1369}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001370static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001371
1372/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001373static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001374{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001375 return sysfs_emit(buf, "%s v%s\n",
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001376 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001377}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001378static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001379
1380/* --------------------------------------------------------------------- */
1381
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001382#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1383
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001384/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001385static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001386{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001387 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001388}
1389
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001390static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1391 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001392{
1393 unsigned long t;
1394
1395 if (parse_strtoul(buf, 1, &t))
1396 return -EINVAL;
1397
Johannes Berg19d337d2009-06-02 13:01:37 +02001398 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001399 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001400 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1401 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001402
1403 return count;
1404}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001405static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001406
1407/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001408static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001409{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001410 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001411}
1412
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001413static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1414 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001415{
1416 unsigned long t;
1417
1418 if (parse_strtoul(buf, 1, &t))
1419 return -EINVAL;
1420
1421 tpacpi_bluetooth_emulstate = !!t;
1422
1423 return count;
1424}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001425static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001426
1427/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001428static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001429{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001430 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001431}
1432
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001433static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1434 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001435{
1436 unsigned long t;
1437
1438 if (parse_strtoul(buf, 1, &t))
1439 return -EINVAL;
1440
1441 tpacpi_wwan_emulstate = !!t;
1442
1443 return count;
1444}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001445static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001446
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001447/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001448static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001449{
Ye Guojin33ce79b2021-10-18 09:17:50 +00001450 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001451}
1452
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001453static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1454 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001455{
1456 unsigned long t;
1457
1458 if (parse_strtoul(buf, 1, &t))
1459 return -EINVAL;
1460
1461 tpacpi_uwb_emulstate = !!t;
1462
1463 return count;
1464}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001465static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001466#endif
1467
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001468/*************************************************************************
1469 * Firmware Data
1470 */
1471
1472/*
1473 * Table of recommended minimum BIOS versions
1474 *
1475 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001476 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001477 * bugs and bad ACPI behaviour on older versions
1478 *
1479 * 2. BIOS or EC fw with known bugs that trigger on Linux
1480 *
1481 * 3. BIOS with known reduced functionality in older versions
1482 *
1483 * We recommend the latest BIOS and EC version.
1484 * We only support the latest BIOS and EC fw version as a rule.
1485 *
1486 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1487 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001488 *
1489 * WARNING: we use this table also to detect that the machine is
1490 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001491 */
1492
1493#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1494 { .vendor = (__v), \
1495 .bios = TPID(__id1, __id2), \
1496 .ec = TPACPI_MATCH_ANY, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001497 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1498 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001499
1500#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001501 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001502 { .vendor = (__v), \
1503 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001504 .ec = __eid, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001505 .quirks = TPVER(__ev1, __ev2) << 16 \
1506 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001507
1508#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1509 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1510
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001511/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001512#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1513 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001514 __bv1, __bv2, TPID(__id1, __id2), \
1515 __ev1, __ev2), \
1516 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1517 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1518 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001519
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001520/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001521#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1522 __eid1, __eid2, __ev1, __ev2) \
1523 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001524 __bv1, __bv2, TPID(__eid1, __eid2), \
1525 __ev1, __ev2), \
1526 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1527 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1528 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001529
1530#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1531 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1532
1533#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1534 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001535 __bv1, __bv2, TPID(__id1, __id2), \
1536 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001537
1538#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1539 __eid1, __eid2, __ev1, __ev2) \
1540 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001541 __bv1, __bv2, TPID(__eid1, __eid2), \
1542 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001543
1544static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1545 /* Numeric models ------------------ */
1546 /* FW MODEL BIOS VERS */
1547 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1548 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1549 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1550 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1551 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1552 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1553 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1554 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1555 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1556
1557 /* A-series ------------------------- */
1558 /* FW MODEL BIOS VERS EC VERS */
1559 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1560 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1561 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1562 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1563 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1564 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1565 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1566 TPV_QI0('1', '3', '2', '0'), /* A22m */
1567 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1568 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1569 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1570
1571 /* G-series ------------------------- */
1572 /* FW MODEL BIOS VERS */
1573 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1574 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1575
1576 /* R-series, T-series --------------- */
1577 /* FW MODEL BIOS VERS EC VERS */
1578 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1579 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1580 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1581 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1582 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1583 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1584 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1585 T40/p, T41/p, T42/p (1) */
1586 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1587 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1588 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1589 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1590
1591 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1592 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1593 TPV_QI0('1', '6', '3', '2'), /* T22 */
1594 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1595 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1596 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1597
1598 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1599 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001600 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001601
1602 /* BIOS FW BIOS VERS EC FW EC VERS */
1603 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1604 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1605
1606 /* X-series ------------------------- */
1607 /* FW MODEL BIOS VERS EC VERS */
1608 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1609 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1610 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1611 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1612 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1613 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1614 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1615
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001616 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1617 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001618
1619 /* (0) - older versions lack DMI EC fw string and functionality */
1620 /* (1) - older versions known to lack functionality */
1621};
1622
1623#undef TPV_QL1
1624#undef TPV_QL0
1625#undef TPV_QI2
1626#undef TPV_QI1
1627#undef TPV_QI0
1628#undef TPV_Q_X
1629#undef TPV_Q
1630
1631static void __init tpacpi_check_outdated_fw(void)
1632{
1633 unsigned long fwvers;
1634 u16 ec_version, bios_version;
1635
1636 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1637 ARRAY_SIZE(tpacpi_bios_version_qtable));
1638
1639 if (!fwvers)
1640 return;
1641
1642 bios_version = fwvers & 0xffffU;
1643 ec_version = (fwvers >> 16) & 0xffffU;
1644
1645 /* note that unknown versions are set to 0x0000 and we use that */
1646 if ((bios_version > thinkpad_id.bios_release) ||
1647 (ec_version > thinkpad_id.ec_release &&
Jouke Witteveen846a4162018-07-11 11:44:41 +02001648 ec_version != TPACPI_MATCH_ANY_VERSION)) {
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001649 /*
1650 * The changelogs would let us track down the exact
1651 * reason, but it is just too much of a pain to track
1652 * it. We only list BIOSes that are either really
1653 * broken, or really stable to begin with, so it is
1654 * best if the user upgrades the firmware anyway.
1655 */
Joe Perches0978e012011-04-04 10:06:25 -07001656 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001657 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 -03001658 }
1659}
1660
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001661static bool __init tpacpi_is_fw_known(void)
1662{
1663 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1664 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1665}
1666
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001667/****************************************************************************
1668 ****************************************************************************
1669 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001670 * Subdrivers
1671 *
1672 ****************************************************************************
1673 ****************************************************************************/
1674
1675/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001676 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001677 */
1678
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001679static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001681 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1682 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001686static struct ibm_struct thinkpad_acpi_driver_data = {
1687 .name = "driver",
1688 .read = thinkpad_acpi_driver_read,
1689};
1690
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001691/*************************************************************************
1692 * Hotkey subdriver
1693 */
1694
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001695/*
1696 * ThinkPad firmware event model
1697 *
1698 * The ThinkPad firmware has two main event interfaces: normal ACPI
1699 * notifications (which follow the ACPI standard), and a private event
1700 * interface.
1701 *
1702 * The private event interface also issues events for the hotkeys. As
1703 * the driver gained features, the event handling code ended up being
1704 * built around the hotkey subdriver. This will need to be refactored
1705 * to a more formal event API eventually.
1706 *
1707 * Some "hotkeys" are actually supposed to be used as event reports,
1708 * such as "brightness has changed", "volume has changed", depending on
1709 * the ThinkPad model and how the firmware is operating.
1710 *
1711 * Unlike other classes, hotkey-class events have mask/unmask control on
1712 * non-ancient firmware. However, how it behaves changes a lot with the
1713 * firmware model and version.
1714 */
1715
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001716enum { /* hot key scan codes (derived from ACPI DSDT) */
1717 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1718 TP_ACPI_HOTKEYSCAN_FNF2,
1719 TP_ACPI_HOTKEYSCAN_FNF3,
1720 TP_ACPI_HOTKEYSCAN_FNF4,
1721 TP_ACPI_HOTKEYSCAN_FNF5,
1722 TP_ACPI_HOTKEYSCAN_FNF6,
1723 TP_ACPI_HOTKEYSCAN_FNF7,
1724 TP_ACPI_HOTKEYSCAN_FNF8,
1725 TP_ACPI_HOTKEYSCAN_FNF9,
1726 TP_ACPI_HOTKEYSCAN_FNF10,
1727 TP_ACPI_HOTKEYSCAN_FNF11,
1728 TP_ACPI_HOTKEYSCAN_FNF12,
1729 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1730 TP_ACPI_HOTKEYSCAN_FNINSERT,
1731 TP_ACPI_HOTKEYSCAN_FNDELETE,
1732 TP_ACPI_HOTKEYSCAN_FNHOME,
1733 TP_ACPI_HOTKEYSCAN_FNEND,
1734 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1735 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1736 TP_ACPI_HOTKEYSCAN_FNSPACE,
1737 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1738 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1739 TP_ACPI_HOTKEYSCAN_MUTE,
1740 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001741 TP_ACPI_HOTKEYSCAN_UNK1,
1742 TP_ACPI_HOTKEYSCAN_UNK2,
1743 TP_ACPI_HOTKEYSCAN_UNK3,
1744 TP_ACPI_HOTKEYSCAN_UNK4,
1745 TP_ACPI_HOTKEYSCAN_UNK5,
1746 TP_ACPI_HOTKEYSCAN_UNK6,
1747 TP_ACPI_HOTKEYSCAN_UNK7,
1748 TP_ACPI_HOTKEYSCAN_UNK8,
1749
Christian Kellner149c8c72017-02-28 17:10:56 +01001750 /* Adaptive keyboard keycodes */
1751 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1752 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001753 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1754 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1755 TP_ACPI_HOTKEYSCAN_CLOUD,
1756 TP_ACPI_HOTKEYSCAN_UNK9,
1757 TP_ACPI_HOTKEYSCAN_VOICE,
1758 TP_ACPI_HOTKEYSCAN_UNK10,
1759 TP_ACPI_HOTKEYSCAN_GESTURES,
1760 TP_ACPI_HOTKEYSCAN_UNK11,
1761 TP_ACPI_HOTKEYSCAN_UNK12,
1762 TP_ACPI_HOTKEYSCAN_UNK13,
1763 TP_ACPI_HOTKEYSCAN_CONFIG,
1764 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1765 TP_ACPI_HOTKEYSCAN_RELOAD,
1766 TP_ACPI_HOTKEYSCAN_BACK,
1767 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1768 TP_ACPI_HOTKEYSCAN_MIC_UP,
1769 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1770 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1771 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1772
Christian Kellner696c65232017-02-28 17:10:57 +01001773 /* Lenovo extended keymap, starting at 0x1300 */
1774 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1775 /* first new observed key (star, favorites) is 0x1311 */
1776 TP_ACPI_HOTKEYSCAN_STAR = 69,
1777 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
Benjamin Bergcb5c1972018-06-20 15:49:27 +02001778 TP_ACPI_HOTKEYSCAN_CALCULATOR,
Christian Kellner696c65232017-02-28 17:10:57 +01001779 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1780 TP_ACPI_HOTKEYSCAN_KEYBOARD,
Hans de Goede7ed77482020-09-08 12:21:11 -07001781 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1782 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1783 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1784 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
Christian Kellner696c65232017-02-28 17:10:57 +01001785
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001786 /* Hotkey keymap size */
1787 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001788};
1789
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001790enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001791 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1792 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1793};
1794
1795enum { /* Positions of some of the keys in hotkey masks */
1796 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1797 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1798 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1799 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1800 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001801 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001802 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1803 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1804 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1805 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1806 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1807};
1808
1809enum { /* NVRAM to ACPI HKEY group map */
1810 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1811 TP_ACPI_HKEY_ZOOM_MASK |
1812 TP_ACPI_HKEY_DISPSWTCH_MASK |
1813 TP_ACPI_HKEY_HIBERNATE_MASK,
1814 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1815 TP_ACPI_HKEY_BRGHTDWN_MASK,
1816 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1817 TP_ACPI_HKEY_VOLDWN_MASK |
1818 TP_ACPI_HKEY_MUTE_MASK,
1819};
1820
1821#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1822struct tp_nvram_state {
1823 u16 thinkpad_toggle:1;
1824 u16 zoom_toggle:1;
1825 u16 display_toggle:1;
1826 u16 thinklight_toggle:1;
1827 u16 hibernate_toggle:1;
1828 u16 displayexp_toggle:1;
1829 u16 display_state:1;
1830 u16 brightness_toggle:1;
1831 u16 volume_toggle:1;
1832 u16 mute:1;
1833
1834 u8 brightness_level;
1835 u8 volume_level;
1836};
1837
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001838/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001839static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001840
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001841/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001842 * Acquire mutex to write poller control variables as an
1843 * atomic block.
1844 *
1845 * Increment hotkey_config_change when changing them if you
1846 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001847 *
1848 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1849 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001850static struct mutex hotkey_thread_data_mutex;
1851static unsigned int hotkey_config_change;
1852
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001853/*
1854 * hotkey poller control variables
1855 *
1856 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001857 *
1858 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1859 * should be used only when the changes need to be taken as
1860 * a block, OR when one needs to force the kthread to forget
1861 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001862 */
1863static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1864static unsigned int hotkey_poll_freq = 10; /* Hz */
1865
1866#define HOTKEY_CONFIG_CRITICAL_START \
1867 do { \
1868 mutex_lock(&hotkey_thread_data_mutex); \
1869 hotkey_config_change++; \
1870 } while (0);
1871#define HOTKEY_CONFIG_CRITICAL_END \
1872 mutex_unlock(&hotkey_thread_data_mutex);
1873
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001874#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1875
1876#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001877#define HOTKEY_CONFIG_CRITICAL_START
1878#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001879
1880#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1881
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001882static struct mutex hotkey_mutex;
1883
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001884static enum { /* Reasons for waking up */
1885 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1886 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1887 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1888} hotkey_wakeup_reason;
1889
1890static int hotkey_autosleep_ack;
1891
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001892static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1893static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04001894static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001895static u32 hotkey_reserved_mask; /* events better left disabled */
1896static u32 hotkey_driver_mask; /* events needed by the driver */
1897static u32 hotkey_user_mask; /* events visible to userspace */
1898static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001899
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001900static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001901
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03001902static void tpacpi_driver_event(const unsigned int hkey_event);
1903static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03001904static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03001905
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001906/* HKEY.MHKG() return bits */
1907#define TP_HOTKEY_TABLET_MASK (1 << 3)
Benjamin Bergdda3ec02017-09-15 15:20:49 +02001908enum {
1909 TP_ACPI_MULTI_MODE_INVALID = 0,
1910 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1911 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1912 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1913 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1914 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1915 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1916 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1917};
1918
1919enum {
1920 /* The following modes are considered tablet mode for the purpose of
1921 * reporting the status to userspace. i.e. in all these modes it makes
1922 * sense to disable the laptop input devices such as touchpad and
1923 * keyboard.
1924 */
1925 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1926 TP_ACPI_MULTI_MODE_STAND |
1927 TP_ACPI_MULTI_MODE_TENT |
1928 TP_ACPI_MULTI_MODE_STAND_TENT,
1929};
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001930
Johannes Berg19d337d2009-06-02 13:01:37 +02001931static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001932{
Johannes Berg19d337d2009-06-02 13:01:37 +02001933 int status;
1934
1935 if (!tp_features.hotkey_wlsw)
1936 return -ENODEV;
1937
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001938#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02001939 if (dbg_wlswemul)
1940 return (tpacpi_wlsw_emulstate) ?
1941 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001942#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02001943
1944 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001945 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001946
1947 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001948}
1949
Benjamin Bergdda3ec02017-09-15 15:20:49 +02001950static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
1951{
1952 int type = (s >> 16) & 0xffff;
1953 int value = s & 0xffff;
1954 int mode = TP_ACPI_MULTI_MODE_INVALID;
1955 int valid_modes = 0;
1956
1957 if (has_tablet_mode)
1958 *has_tablet_mode = 0;
1959
1960 switch (type) {
1961 case 1:
1962 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1963 TP_ACPI_MULTI_MODE_TABLET |
1964 TP_ACPI_MULTI_MODE_STAND_TENT;
1965 break;
1966 case 2:
1967 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1968 TP_ACPI_MULTI_MODE_FLAT |
1969 TP_ACPI_MULTI_MODE_TABLET |
1970 TP_ACPI_MULTI_MODE_STAND |
1971 TP_ACPI_MULTI_MODE_TENT;
1972 break;
1973 case 3:
1974 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1975 TP_ACPI_MULTI_MODE_FLAT;
1976 break;
1977 case 4:
Benjamin Bergdda3ec02017-09-15 15:20:49 +02001978 case 5:
Benjamin Berg26befef2017-11-14 17:14:14 +01001979 /* In mode 4, FLAT is not specified as a valid mode. However,
1980 * it can be seen at least on the X1 Yoga 2nd Generation.
1981 */
Benjamin Bergdda3ec02017-09-15 15:20:49 +02001982 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1983 TP_ACPI_MULTI_MODE_FLAT |
1984 TP_ACPI_MULTI_MODE_TABLET |
1985 TP_ACPI_MULTI_MODE_STAND |
1986 TP_ACPI_MULTI_MODE_TENT;
1987 break;
1988 default:
1989 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
1990 type, value, TPACPI_MAIL);
1991 return 0;
1992 }
1993
1994 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
1995 *has_tablet_mode = 1;
1996
1997 switch (value) {
1998 case 1:
1999 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2000 break;
2001 case 2:
2002 mode = TP_ACPI_MULTI_MODE_FLAT;
2003 break;
2004 case 3:
2005 mode = TP_ACPI_MULTI_MODE_TABLET;
2006 break;
2007 case 4:
2008 if (type == 1)
2009 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2010 else
2011 mode = TP_ACPI_MULTI_MODE_STAND;
2012 break;
2013 case 5:
2014 mode = TP_ACPI_MULTI_MODE_TENT;
2015 break;
2016 default:
2017 if (type == 5 && value == 0xffff) {
2018 pr_warn("Multi mode status is undetected, assuming laptop\n");
2019 return 0;
2020 }
2021 }
2022
2023 if (!(mode & valid_modes)) {
2024 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2025 value, type, TPACPI_MAIL);
2026 return 0;
2027 }
2028
2029 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2030}
2031
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002032static int hotkey_get_tablet_mode(int *status)
2033{
2034 int s;
2035
Lyudeb03f4d42016-11-11 15:15:03 -05002036 switch (tp_features.hotkey_tablet) {
2037 case TP_HOTKEY_TABLET_USES_MHKG:
2038 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2039 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002040
Lyudeb03f4d42016-11-11 15:15:03 -05002041 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2042 break;
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002043 case TP_HOTKEY_TABLET_USES_GMMS:
2044 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
Lyudeb03f4d42016-11-11 15:15:03 -05002045 return -EIO;
2046
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002047 *status = hotkey_gmms_get_tablet_mode(s, NULL);
Lyudeb03f4d42016-11-11 15:15:03 -05002048 break;
2049 default:
2050 break;
2051 }
2052
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002053 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002054}
2055
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002056/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002057 * Reads current event mask from firmware, and updates
2058 * hotkey_acpi_mask accordingly. Also resets any bits
2059 * from hotkey_user_mask that are unavailable to be
2060 * delivered (shadow requirement of the userspace ABI).
2061 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002062 * Call with hotkey_mutex held
2063 */
2064static int hotkey_mask_get(void)
2065{
2066 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002067 u32 m = 0;
2068
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002069 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002070 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002071
2072 hotkey_acpi_mask = m;
2073 } else {
2074 /* no mask support doesn't mean no event support... */
2075 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002076 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002077
2078 /* sync userspace-visible mask */
2079 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002080
2081 return 0;
2082}
2083
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002084static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002085{
2086 /* log only what the user can fix... */
2087 const u32 wantedmask = hotkey_driver_mask &
2088 ~(hotkey_acpi_mask | hotkey_source_mask) &
2089 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2090
2091 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002092 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002093}
2094
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002095/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002096 * Set the firmware mask when supported
2097 *
2098 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2099 *
2100 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2101 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002102 * Call with hotkey_mutex held
2103 */
2104static int hotkey_mask_set(u32 mask)
2105{
2106 int i;
2107 int rc = 0;
2108
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002109 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002110
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002111 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002112 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002113 if (!acpi_evalf(hkey_handle,
2114 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002115 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002116 rc = -EIO;
2117 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002118 }
2119 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002120 }
2121
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002122 /*
2123 * We *must* make an inconditional call to hotkey_mask_get to
2124 * refresh hotkey_acpi_mask and update hotkey_user_mask
2125 *
2126 * Take the opportunity to also log when we cannot _enable_
2127 * a given event.
2128 */
2129 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002130 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002131 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002132 }
2133
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002134 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2135 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002136
2137 return rc;
2138}
2139
2140/*
2141 * Sets hotkey_user_mask and tries to set the firmware mask
2142 *
2143 * Call with hotkey_mutex held
2144 */
2145static int hotkey_user_mask_set(const u32 mask)
2146{
2147 int rc;
2148
2149 /* Give people a chance to notice they are doing something that
2150 * is bound to go boom on their users sooner or later */
2151 if (!tp_warned.hotkey_mask_ff &&
2152 (mask == 0xffff || mask == 0xffffff ||
2153 mask == 0xffffffff)) {
2154 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002155 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2156 mask);
2157 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 -03002158 }
2159
2160 /* Try to enable what the user asked for, plus whatever we need.
2161 * this syncs everything but won't enable bits in hotkey_user_mask */
2162 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2163
2164 /* Enable the available bits in hotkey_user_mask */
2165 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2166
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002167 return rc;
2168}
2169
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002170/*
2171 * Sets the driver hotkey mask.
2172 *
2173 * Can be called even if the hotkey subdriver is inactive
2174 */
2175static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2176{
2177 int rc;
2178
2179 /* Do the right thing if hotkey_init has not been called yet */
2180 if (!tp_features.hotkey) {
2181 hotkey_driver_mask = mask;
2182 return 0;
2183 }
2184
2185 mutex_lock(&hotkey_mutex);
2186
2187 HOTKEY_CONFIG_CRITICAL_START
2188 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002189#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002190 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002191#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002192 HOTKEY_CONFIG_CRITICAL_END
2193
2194 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2195 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002196 hotkey_poll_setup(true);
2197
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002198 mutex_unlock(&hotkey_mutex);
2199
2200 return rc;
2201}
2202
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002203static int hotkey_status_get(int *status)
2204{
2205 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2206 return -EIO;
2207
2208 return 0;
2209}
2210
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002211static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002212{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002213 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002214 return -EIO;
2215
2216 return 0;
2217}
2218
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002219static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002220{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002221 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002222
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002223 if (tp_features.hotkey_tablet &&
2224 !hotkey_get_tablet_mode(&state)) {
2225 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002226
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002227 input_report_switch(tpacpi_inputdev,
2228 SW_TABLET_MODE, !!state);
2229 input_sync(tpacpi_inputdev);
2230
2231 mutex_unlock(&tpacpi_inputdev_send_mutex);
2232 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002233}
2234
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002235/* Do NOT call without validating scancode first */
2236static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002237{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002238 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002239
2240 if (keycode != KEY_RESERVED) {
2241 mutex_lock(&tpacpi_inputdev_send_mutex);
2242
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002243 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002244 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002245 input_sync(tpacpi_inputdev);
2246
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002247 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002248 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002249 input_sync(tpacpi_inputdev);
2250
2251 mutex_unlock(&tpacpi_inputdev_send_mutex);
2252 }
2253}
2254
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002255/* Do NOT call without validating scancode first */
2256static void tpacpi_input_send_key_masked(const unsigned int scancode)
2257{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002258 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002259 if (hotkey_user_mask & (1 << scancode))
2260 tpacpi_input_send_key(scancode);
2261}
2262
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002263#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2264static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2265
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002266/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002267static void tpacpi_hotkey_send_key(unsigned int scancode)
2268{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002269 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002270}
2271
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002272static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002273{
2274 u8 d;
2275
2276 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2277 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2278 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2279 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2280 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2281 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2282 }
Hans de Goedec685e202017-02-09 16:44:13 +01002283 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002284 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2285 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2286 }
2287 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2288 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2289 n->displayexp_toggle =
2290 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2291 }
2292 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2293 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2294 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2295 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2296 n->brightness_toggle =
2297 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2298 }
2299 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2300 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2301 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2302 >> TP_NVRAM_POS_LEVEL_VOLUME;
2303 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2304 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2305 }
2306}
2307
Behan Webstera4d44ba2014-02-12 21:58:46 +01002308#define TPACPI_COMPARE_KEY(__scancode, __member) \
2309do { \
2310 if ((event_mask & (1 << __scancode)) && \
2311 oldn->__member != newn->__member) \
2312 tpacpi_hotkey_send_key(__scancode); \
2313} while (0)
2314
2315#define TPACPI_MAY_SEND_KEY(__scancode) \
2316do { \
2317 if (event_mask & (1 << __scancode)) \
2318 tpacpi_hotkey_send_key(__scancode); \
2319} while (0)
2320
2321static void issue_volchange(const unsigned int oldvol,
2322 const unsigned int newvol,
2323 const u32 event_mask)
2324{
2325 unsigned int i = oldvol;
2326
2327 while (i > newvol) {
2328 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2329 i--;
2330 }
2331 while (i < newvol) {
2332 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2333 i++;
2334 }
2335}
2336
2337static void issue_brightnesschange(const unsigned int oldbrt,
2338 const unsigned int newbrt,
2339 const u32 event_mask)
2340{
2341 unsigned int i = oldbrt;
2342
2343 while (i > newbrt) {
2344 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2345 i--;
2346 }
2347 while (i < newbrt) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2349 i++;
2350 }
2351}
2352
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002353static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2354 struct tp_nvram_state *newn,
2355 const u32 event_mask)
2356{
2357
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002358 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2359 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2360 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2361 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2362
2363 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2364
2365 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2366
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002367 /*
2368 * Handle volume
2369 *
2370 * This code is supposed to duplicate the IBM firmware behaviour:
2371 * - Pressing MUTE issues mute hotkey message, even when already mute
2372 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002373 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002374 * - The act of unmuting issues volume up/down notification,
2375 * depending which key was used to unmute
2376 *
2377 * We are constrained to what the NVRAM can tell us, which is not much
2378 * and certainly not enough if more than one volume hotkey was pressed
2379 * since the last poll cycle.
2380 *
2381 * Just to make our life interesting, some newer Lenovo ThinkPads have
2382 * bugs in the BIOS and may fail to update volume_toggle properly.
2383 */
2384 if (newn->mute) {
2385 /* muted */
2386 if (!oldn->mute ||
2387 oldn->volume_toggle != newn->volume_toggle ||
2388 oldn->volume_level != newn->volume_level) {
2389 /* recently muted, or repeated mute keypress, or
2390 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002391 issue_volchange(oldn->volume_level, newn->volume_level,
2392 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002393 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2394 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002395 } else {
2396 /* unmute */
2397 if (oldn->mute) {
2398 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002399 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002400 }
2401 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002402 issue_volchange(oldn->volume_level, newn->volume_level,
2403 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002404 } else if (oldn->volume_toggle != newn->volume_toggle) {
2405 /* repeated vol up/down keypress at end of scale ? */
2406 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002408 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002410 }
2411 }
2412
2413 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002414 if (oldn->brightness_level != newn->brightness_level) {
2415 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002416 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002417 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2418 /* repeated key presses that didn't change state */
2419 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002421 else if (newn->brightness_level >= bright_maxlvl
2422 && !tp_features.bright_unkfw)
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002424 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002425
2426#undef TPACPI_COMPARE_KEY
2427#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002428}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002429
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002430/*
2431 * Polling driver
2432 *
2433 * We track all events in hotkey_source_mask all the time, since
2434 * most of them are edge-based. We only issue those requested by
2435 * hotkey_user_mask or hotkey_driver_mask, though.
2436 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002437static int hotkey_kthread(void *data)
2438{
Tom Rix5f38b062020-09-13 12:02:03 -07002439 struct tp_nvram_state s[2] = { 0 };
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002440 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002441 unsigned int si, so;
2442 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002443 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002444 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002445 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002446
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2448 goto exit;
2449
2450 set_freezable();
2451
2452 so = 0;
2453 si = 1;
2454 t = 0;
2455
2456 /* Initial state for compares */
2457 mutex_lock(&hotkey_thread_data_mutex);
2458 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002459 poll_mask = hotkey_source_mask;
2460 event_mask = hotkey_source_mask &
2461 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002462 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002463 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002464 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002465
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002466 while (!kthread_should_stop()) {
2467 if (t == 0) {
2468 if (likely(poll_freq))
2469 t = 1000/poll_freq;
2470 else
2471 t = 100; /* should never happen... */
2472 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002473 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002474 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002475 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002476
2477 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478 continue;
2479
2480 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002481 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002482 /* forget old state on thaw or config change */
2483 si = so;
2484 t = 0;
2485 change_detector = hotkey_config_change;
2486 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002487 poll_mask = hotkey_source_mask;
2488 event_mask = hotkey_source_mask &
2489 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002490 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 mutex_unlock(&hotkey_thread_data_mutex);
2492
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002493 if (likely(poll_mask)) {
2494 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002495 if (likely(si != so)) {
2496 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002497 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002498 }
2499 }
2500
2501 so = si;
2502 si ^= 1;
2503 }
2504
2505exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002506 return 0;
2507}
2508
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002509/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510static void hotkey_poll_stop_sync(void)
2511{
2512 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002513 kthread_stop(tpacpi_hotkey_task);
2514 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 }
2516}
2517
2518/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002519static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002520{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002521 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2522 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002523
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002524 if (hotkey_poll_freq > 0 &&
2525 (poll_driver_mask ||
2526 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002527 if (!tpacpi_hotkey_task) {
2528 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002529 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002530 if (IS_ERR(tpacpi_hotkey_task)) {
2531 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002532 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002533 }
2534 }
2535 } else {
2536 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002537 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002538 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002539 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 -07002540 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002541 }
2542 }
2543}
2544
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002545static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002546{
2547 mutex_lock(&hotkey_mutex);
2548 hotkey_poll_setup(may_warn);
2549 mutex_unlock(&hotkey_mutex);
2550}
2551
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002552/* call with hotkey_mutex held */
2553static void hotkey_poll_set_freq(unsigned int freq)
2554{
2555 if (!freq)
2556 hotkey_poll_stop_sync();
2557
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002558 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002559}
2560
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002561#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2562
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002563static void hotkey_poll_setup(const bool __unused)
2564{
2565}
2566
2567static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002568{
2569}
2570
2571#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2572
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573static int hotkey_inputdev_open(struct input_dev *dev)
2574{
2575 switch (tpacpi_lifecycle) {
2576 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002577 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002578 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002579 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002580 case TPACPI_LIFE_EXITING:
2581 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582 }
2583
2584 /* Should only happen if tpacpi_lifecycle is corrupt */
2585 BUG();
2586 return -EBUSY;
2587}
2588
2589static void hotkey_inputdev_close(struct input_dev *dev)
2590{
2591 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002592 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002593 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002594 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002595}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002596
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002597/* sysfs hotkey enable ------------------------------------------------- */
2598static ssize_t hotkey_enable_show(struct device *dev,
2599 struct device_attribute *attr,
2600 char *buf)
2601{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002602 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002603
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002604 printk_deprecated_attribute("hotkey_enable",
2605 "Hotkey reporting is always enabled");
2606
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002607 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002608 if (res)
2609 return res;
2610
Ye Guojin33ce79b2021-10-18 09:17:50 +00002611 return sysfs_emit(buf, "%d\n", status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002612}
2613
2614static ssize_t hotkey_enable_store(struct device *dev,
2615 struct device_attribute *attr,
2616 const char *buf, size_t count)
2617{
2618 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002619
2620 printk_deprecated_attribute("hotkey_enable",
2621 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002622
2623 if (parse_strtoul(buf, 1, &t))
2624 return -EINVAL;
2625
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002626 if (t == 0)
2627 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002628
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002629 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002630}
2631
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002632static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002633
2634/* sysfs hotkey mask --------------------------------------------------- */
2635static ssize_t hotkey_mask_show(struct device *dev,
2636 struct device_attribute *attr,
2637 char *buf)
2638{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002639 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002640}
2641
2642static ssize_t hotkey_mask_store(struct device *dev,
2643 struct device_attribute *attr,
2644 const char *buf, size_t count)
2645{
2646 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002647 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002648
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002649 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002650 return -EINVAL;
2651
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002652 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002653 return -ERESTARTSYS;
2654
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002655 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002656
2657#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002658 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002659#endif
2660
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002661 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002662
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002663 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2664
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002665 return (res) ? res : count;
2666}
2667
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002668static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002669
2670/* sysfs hotkey bios_enabled ------------------------------------------- */
2671static ssize_t hotkey_bios_enabled_show(struct device *dev,
2672 struct device_attribute *attr,
2673 char *buf)
2674{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002675 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002676}
2677
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002678static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002679
2680/* sysfs hotkey bios_mask ---------------------------------------------- */
2681static ssize_t hotkey_bios_mask_show(struct device *dev,
2682 struct device_attribute *attr,
2683 char *buf)
2684{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002685 printk_deprecated_attribute("hotkey_bios_mask",
2686 "This attribute is useless.");
Ye Guojin33ce79b2021-10-18 09:17:50 +00002687 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688}
2689
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002690static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002692/* sysfs hotkey all_mask ----------------------------------------------- */
2693static ssize_t hotkey_all_mask_show(struct device *dev,
2694 struct device_attribute *attr,
2695 char *buf)
2696{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002697 return sysfs_emit(buf, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002698 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002699}
2700
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002701static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002702
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002703/* sysfs hotkey all_mask ----------------------------------------------- */
2704static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2705 struct device_attribute *attr,
2706 char *buf)
2707{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002708 return sysfs_emit(buf, "0x%08x\n",
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002709 hotkey_adaptive_all_mask | hotkey_source_mask);
2710}
2711
2712static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2713
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002714/* sysfs hotkey recommended_mask --------------------------------------- */
2715static ssize_t hotkey_recommended_mask_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2718{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002719 return sysfs_emit(buf, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002720 (hotkey_all_mask | hotkey_source_mask)
2721 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002722}
2723
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002724static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002725
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002726#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2727
2728/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2729static ssize_t hotkey_source_mask_show(struct device *dev,
2730 struct device_attribute *attr,
2731 char *buf)
2732{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002733 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002734}
2735
2736static ssize_t hotkey_source_mask_store(struct device *dev,
2737 struct device_attribute *attr,
2738 const char *buf, size_t count)
2739{
2740 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002741 u32 r_ev;
2742 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002743
2744 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2745 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2746 return -EINVAL;
2747
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002748 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002749 return -ERESTARTSYS;
2750
2751 HOTKEY_CONFIG_CRITICAL_START
2752 hotkey_source_mask = t;
2753 HOTKEY_CONFIG_CRITICAL_END
2754
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002755 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2756 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002757 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002758
2759 /* check if events needed by the driver got disabled */
2760 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2761 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002762
2763 mutex_unlock(&hotkey_mutex);
2764
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002765 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002766 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002767
2768 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002769 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002770 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002771
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002772 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2773
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002774 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002775}
2776
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002777static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002778
2779/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2780static ssize_t hotkey_poll_freq_show(struct device *dev,
2781 struct device_attribute *attr,
2782 char *buf)
2783{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002784 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002785}
2786
2787static ssize_t hotkey_poll_freq_store(struct device *dev,
2788 struct device_attribute *attr,
2789 const char *buf, size_t count)
2790{
2791 unsigned long t;
2792
2793 if (parse_strtoul(buf, 25, &t))
2794 return -EINVAL;
2795
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002796 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002797 return -ERESTARTSYS;
2798
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002799 hotkey_poll_set_freq(t);
2800 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002801
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002802 mutex_unlock(&hotkey_mutex);
2803
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002804 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2805
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002806 return count;
2807}
2808
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002809static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002810
2811#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2812
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002813/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002814static ssize_t hotkey_radio_sw_show(struct device *dev,
2815 struct device_attribute *attr,
2816 char *buf)
2817{
Johannes Berg19d337d2009-06-02 13:01:37 +02002818 int res;
2819 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002820 if (res < 0)
2821 return res;
2822
Johannes Berg19d337d2009-06-02 13:01:37 +02002823 /* Opportunistic update */
2824 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2825
Ye Guojin33ce79b2021-10-18 09:17:50 +00002826 return sysfs_emit(buf, "%d\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02002827 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002828}
2829
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002830static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002831
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002832static void hotkey_radio_sw_notify_change(void)
2833{
2834 if (tp_features.hotkey_wlsw)
2835 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2836 "hotkey_radio_sw");
2837}
2838
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002839/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2840static ssize_t hotkey_tablet_mode_show(struct device *dev,
2841 struct device_attribute *attr,
2842 char *buf)
2843{
2844 int res, s;
2845 res = hotkey_get_tablet_mode(&s);
2846 if (res < 0)
2847 return res;
2848
Ye Guojin33ce79b2021-10-18 09:17:50 +00002849 return sysfs_emit(buf, "%d\n", !!s);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002850}
2851
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002852static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002853
2854static void hotkey_tablet_mode_notify_change(void)
2855{
2856 if (tp_features.hotkey_tablet)
2857 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2858 "hotkey_tablet_mode");
2859}
2860
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002861/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002862static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2863 struct device_attribute *attr,
2864 char *buf)
2865{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002866 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002867}
2868
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002869static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002870
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002871static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002872{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002873 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2874 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002875}
2876
2877/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002878static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2879 struct device_attribute *attr,
2880 char *buf)
2881{
Ye Guojin33ce79b2021-10-18 09:17:50 +00002882 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002883}
2884
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002885static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2886 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002887
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002888static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002889{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002890 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2891 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002892}
2893
Bastien Nocerab790cee2015-03-02 14:45:27 +01002894/* sysfs adaptive kbd mode --------------------------------------------- */
2895
2896static int adaptive_keyboard_get_mode(void);
2897static int adaptive_keyboard_set_mode(int new_mode);
2898
2899enum ADAPTIVE_KEY_MODE {
2900 HOME_MODE,
2901 WEB_BROWSER_MODE,
2902 WEB_CONFERENCE_MODE,
2903 FUNCTION_MODE,
2904 LAYFLAT_MODE
2905};
2906
2907static ssize_t adaptive_kbd_mode_show(struct device *dev,
2908 struct device_attribute *attr,
2909 char *buf)
2910{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002911 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002912
2913 current_mode = adaptive_keyboard_get_mode();
2914 if (current_mode < 0)
2915 return current_mode;
2916
Ye Guojin33ce79b2021-10-18 09:17:50 +00002917 return sysfs_emit(buf, "%d\n", current_mode);
Bastien Nocerab790cee2015-03-02 14:45:27 +01002918}
2919
2920static ssize_t adaptive_kbd_mode_store(struct device *dev,
2921 struct device_attribute *attr,
2922 const char *buf, size_t count)
2923{
2924 unsigned long t;
2925 int res;
2926
2927 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2928 return -EINVAL;
2929
2930 res = adaptive_keyboard_set_mode(t);
2931 return (res < 0) ? res : count;
2932}
2933
2934static DEVICE_ATTR_RW(adaptive_kbd_mode);
2935
2936static struct attribute *adaptive_kbd_attributes[] = {
2937 &dev_attr_adaptive_kbd_mode.attr,
2938 NULL
2939};
2940
Len Baker79f960e2021-10-23 17:40:36 +02002941static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
2942 struct attribute *attr, int n)
2943{
2944 return tp_features.has_adaptive_kbd ? attr->mode : 0;
2945}
2946
Bastien Nocerab790cee2015-03-02 14:45:27 +01002947static const struct attribute_group adaptive_kbd_attr_group = {
Len Baker79f960e2021-10-23 17:40:36 +02002948 .is_visible = hadaptive_kbd_attr_is_visible,
Bastien Nocerab790cee2015-03-02 14:45:27 +01002949 .attrs = adaptive_kbd_attributes,
2950};
2951
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002952/* --------------------------------------------------------------------- */
2953
Len Bakerc99ca782021-09-26 13:19:08 +02002954static struct attribute *hotkey_attributes[] = {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002955 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002956 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002957 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002958 &dev_attr_wakeup_reason.attr,
2959 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002960 &dev_attr_hotkey_mask.attr,
2961 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002962 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002963 &dev_attr_hotkey_recommended_mask.attr,
Hans de Goedeb68f8a12021-11-23 22:04:19 +01002964 &dev_attr_hotkey_tablet_mode.attr,
2965 &dev_attr_hotkey_radio_sw.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002966#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002967 &dev_attr_hotkey_source_mask.attr,
2968 &dev_attr_hotkey_poll_freq.attr,
2969#endif
Len Bakerc99ca782021-09-26 13:19:08 +02002970 NULL
2971};
2972
2973static umode_t hotkey_attr_is_visible(struct kobject *kobj,
2974 struct attribute *attr, int n)
2975{
2976 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
2977 if (!tp_features.hotkey_tablet)
2978 return 0;
2979 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
2980 if (!tp_features.hotkey_wlsw)
2981 return 0;
2982 }
2983
2984 return attr->mode;
2985}
2986
2987static const struct attribute_group hotkey_attr_group = {
2988 .is_visible = hotkey_attr_is_visible,
2989 .attrs = hotkey_attributes,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002990};
2991
Johannes Berg19d337d2009-06-02 13:01:37 +02002992/*
2993 * Sync both the hw and sw blocking state of all switches
2994 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002995static void tpacpi_send_radiosw_update(void)
2996{
2997 int wlsw;
2998
Johannes Berg19d337d2009-06-02 13:01:37 +02002999 /*
3000 * We must sync all rfkill controllers *before* issuing any
3001 * rfkill input events, or we will race the rfkill core input
3002 * handler.
3003 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003004 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003005 * for the above.
3006 *
3007 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3008 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003009
Johannes Berg19d337d2009-06-02 13:01:37 +02003010 wlsw = hotkey_get_wlsw();
3011
3012 /* Sync hw blocking state first if it is hw-blocked */
3013 if (wlsw == TPACPI_RFK_RADIO_OFF)
3014 tpacpi_rfk_update_hwblock_state(true);
3015
Johannes Berg19d337d2009-06-02 13:01:37 +02003016 /* Sync hw blocking state last if it is hw-unblocked */
3017 if (wlsw == TPACPI_RFK_RADIO_ON)
3018 tpacpi_rfk_update_hwblock_state(false);
3019
3020 /* Issue rfkill input event for WLSW switch */
3021 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003022 mutex_lock(&tpacpi_inputdev_send_mutex);
3023
3024 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003025 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003026 input_sync(tpacpi_inputdev);
3027
3028 mutex_unlock(&tpacpi_inputdev_send_mutex);
3029 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003030
3031 /*
3032 * this can be unconditional, as we will poll state again
3033 * if userspace uses the notify to read data
3034 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003035 hotkey_radio_sw_notify_change();
3036}
3037
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003038static void hotkey_exit(void)
3039{
3040#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003041 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003042 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003043 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003044#endif
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003045 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003046 "restoring original HKEY status and mask\n");
3047 /* yes, there is a bitwise or below, we want the
3048 * functions to be called even if one of them fail */
3049 if (((tp_features.hotkey_mask &&
3050 hotkey_mask_set(hotkey_orig_mask)) |
3051 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003052 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003053}
3054
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003055static void __init hotkey_unmap(const unsigned int scancode)
3056{
3057 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3058 clear_bit(hotkey_keycode_map[scancode],
3059 tpacpi_inputdev->keybit);
3060 hotkey_keycode_map[scancode] = KEY_RESERVED;
3061 }
3062}
3063
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003064/*
3065 * HKEY quirks:
3066 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3067 */
3068
3069#define TPACPI_HK_Q_INIMASK 0x0001
3070
3071static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3072 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3073 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3074 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3075 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3076 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3077 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3078 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3079 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3080 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3081 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3082 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3083 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3084 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3085 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3086 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3087 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3088 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3089 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3090 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3091};
3092
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003093typedef u16 tpacpi_keymap_entry_t;
3094typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003095
Lyudeb3180022016-11-11 15:15:02 -05003096static int hotkey_init_tablet_mode(void)
3097{
Darren Hartcb2bf252016-12-14 14:45:48 -08003098 int in_tablet_mode = 0, res;
3099 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003100
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003101 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3102 int has_tablet_mode;
3103
3104 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3105 &has_tablet_mode);
Hans de Goede685489a2020-11-06 15:01:30 +01003106 /*
3107 * The Yoga 11e series has 2 accelerometers described by a
3108 * BOSC0200 ACPI node. This setup relies on a Windows service
3109 * which calls special ACPI methods on this node to report
3110 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3111 * does not support this, so skip the hotkey on these models.
3112 */
Hans de Goede153cca92021-07-29 10:21:34 +02003113 if (has_tablet_mode && !dual_accel_detect())
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003114 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3115 type = "GMMS";
3116 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003117 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003118 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3119 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3120 type = "MHKG";
3121 }
3122
3123 if (!tp_features.hotkey_tablet)
3124 return 0;
3125
3126 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3127 type, in_tablet_mode ? "tablet" : "laptop");
3128
Lyudeb3180022016-11-11 15:15:02 -05003129 return in_tablet_mode;
3130}
3131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003132static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003133{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003134 /* Requirements for changing the default keymaps:
3135 *
3136 * 1. Many of the keys are mapped to KEY_RESERVED for very
3137 * good reasons. Do not change them unless you have deep
3138 * knowledge on the IBM and Lenovo ThinkPad firmware for
3139 * the various ThinkPad models. The driver behaves
3140 * differently for KEY_RESERVED: such keys have their
3141 * hot key mask *unset* in mask_recommended, and also
3142 * in the initial hot key mask programmed into the
3143 * firmware at driver load time, which means the firm-
3144 * ware may react very differently if you change them to
3145 * something else;
3146 *
3147 * 2. You must be subscribed to the linux-thinkpad and
3148 * ibm-acpi-devel mailing lists, and you should read the
3149 * list archives since 2007 if you want to change the
3150 * keymaps. This requirement exists so that you will
3151 * know the past history of problems with the thinkpad-
3152 * acpi driver keymaps, and also that you will be
3153 * listening to any bug reports;
3154 *
3155 * 3. Do not send thinkpad-acpi specific patches directly to
3156 * for merging, *ever*. Send them to the linux-acpi
3157 * mailinglist for comments. Merging is to be done only
3158 * through acpi-test and the ACPI maintainer.
3159 *
3160 * If the above is too much to ask, don't change the keymap.
3161 * Ask the thinkpad-acpi maintainer to do it, instead.
3162 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003163
3164 enum keymap_index {
3165 TPACPI_KEYMAP_IBM_GENERIC = 0,
3166 TPACPI_KEYMAP_LENOVO_GENERIC,
3167 };
3168
3169 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3170 /* Generic keymap for IBM ThinkPads */
3171 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003172 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003173 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003174 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3175 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003176
3177 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003178 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3179 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3180 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003181
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003182 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003183 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003184 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003185
3186 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003187 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003188
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003189 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3190 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003191
3192 /* Volume: firmware always react to it and reprograms
3193 * the built-in *extra* mixer. Never map it to control
3194 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003195 KEY_RESERVED, /* 0x14: VOLUME UP */
3196 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3197 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003198
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003199 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003200
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003201 /* (assignments unknown, please report if found) */
3202 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3203 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003204
3205 /* No assignments, only used for Adaptive keyboards. */
3206 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3207 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3208 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3209 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3210 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003211
3212 /* No assignment, used for newer Lenovo models */
3213 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3214 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3215 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3218 KEY_UNKNOWN, KEY_UNKNOWN
3219
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003220 },
3221
3222 /* Generic keymap for Lenovo ThinkPads */
3223 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003224 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3225 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003226 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003227 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003228
3229 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003230 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3231 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3232 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003233
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003234 /* These should be enabled --only-- when ACPI video
3235 * is disabled (i.e. in "vendor" mode), and are handled
3236 * in a special way by the init code */
3237 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3238 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003239
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003240 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003241
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003242 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3243 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003244
3245 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3246 * react to it and reprograms the built-in *extra* mixer.
3247 * Never map it to control another mixer by default.
3248 *
3249 * T60?, T61, R60?, R61: firmware and EC tries to send
3250 * these over the regular keyboard, so these are no-ops,
3251 * but there are still weird bugs re. MUTE, so do not
3252 * change unless you get test reports from all Lenovo
3253 * models. May cause the BIOS to interfere with the
3254 * HDA mixer.
3255 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003256 KEY_RESERVED, /* 0x14: VOLUME UP */
3257 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3258 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003259
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003260 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003261
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003262 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003263 KEY_UNKNOWN, KEY_UNKNOWN,
3264
3265 /*
3266 * The mic mute button only sends 0x1a. It does not
3267 * automatically mute the mic or change the mute light.
3268 */
3269 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3270
3271 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003272 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003273
3274 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003275 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003276
3277 /*
3278 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3279 * The first item in this list is the Mute button which is
3280 * emitted with 0x103 through
3281 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3282 * symbol is held.
3283 * We'll need to offset those by 0x20.
3284 */
3285 KEY_RESERVED, /* Mute held, 0x103 */
3286 KEY_BRIGHTNESS_MIN, /* Backlight off */
3287 KEY_RESERVED, /* Clipping tool */
3288 KEY_RESERVED, /* Cloud */
3289 KEY_RESERVED,
3290 KEY_VOICECOMMAND, /* Voice */
3291 KEY_RESERVED,
3292 KEY_RESERVED, /* Gestures */
3293 KEY_RESERVED,
3294 KEY_RESERVED,
3295 KEY_RESERVED,
3296 KEY_CONFIG, /* Settings */
3297 KEY_RESERVED, /* New tab */
3298 KEY_REFRESH, /* Reload */
3299 KEY_BACK, /* Back */
3300 KEY_RESERVED, /* Microphone down */
3301 KEY_RESERVED, /* Microphone up */
3302 KEY_RESERVED, /* Microphone cancellation */
3303 KEY_RESERVED, /* Camera mode */
3304 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003305
3306 /*
3307 * These are found in 2017 models (e.g. T470s, X270).
3308 * The lowest known value is 0x311, which according to
3309 * the manual should launch a user defined favorite
3310 * application.
3311 *
3312 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3313 * corresponding to 0x34.
3314 */
3315
3316 /* (assignments unknown, please report if found) */
3317 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3318 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3319 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3320 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3321 KEY_UNKNOWN,
3322
Hans de Goede7ed77482020-09-08 12:21:11 -07003323 KEY_BOOKMARKS, /* Favorite app, 0x311 */
Hans de Goedee2c8c4e2020-09-08 12:21:50 -07003324 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
Hans de Goede7ed77482020-09-08 12:21:11 -07003325 KEY_CALC, /* Calculator (above numpad, P52) */
3326 KEY_BLUETOOTH, /* Bluetooth */
3327 KEY_KEYBOARD, /* Keyboard, 0x315 */
3328 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3329 KEY_NOTIFICATION_CENTER, /* Notification Center */
3330 KEY_PICKUP_PHONE, /* Answer incoming call */
3331 KEY_HANGUP_PHONE, /* Decline incoming call */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003332 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003333 };
3334
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003335 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3336 /* Generic maps (fallback) */
3337 {
3338 .vendor = PCI_VENDOR_ID_IBM,
3339 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3340 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3341 },
3342 {
3343 .vendor = PCI_VENDOR_ID_LENOVO,
3344 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3345 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3346 },
3347 };
3348
3349#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003350#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003351
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003352 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003353 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003354 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003355 bool radiosw_state = false;
3356 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003357
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003358 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003359 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003360
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003361 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3362 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003363
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003364 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003365 BUG_ON(tpacpi_inputdev->open != NULL ||
3366 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003367
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003368 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003369 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003370
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003371#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003372 mutex_init(&hotkey_thread_data_mutex);
3373#endif
3374
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003375 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003376 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003377
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003378 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3379 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003380 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003381
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382 if (!tp_features.hotkey)
Hans de Goedec7e1c782021-11-21 20:11:24 +01003383 return -ENODEV;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003384
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003385 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3386 ARRAY_SIZE(tpacpi_hotkey_qtable));
3387
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003388 tpacpi_disable_brightness_delay();
3389
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003390 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003391 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3392 for HKEY interface version 0x100 */
3393 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003394 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3395 "firmware HKEY interface version: 0x%x\n",
3396 hkeyv);
3397
3398 switch (hkeyv >> 8) {
3399 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003400 /*
3401 * MHKV 0x100 in A31, R40, R40e,
3402 * T4x, X31, and later
3403 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003404
3405 /* Paranoia check AND init hotkey_all_mask */
3406 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3407 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003408 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003409 TPACPI_MAIL);
3410 /* Fallback: pre-init for FN+F3,F4,F12 */
3411 hotkey_all_mask = 0x080cU;
3412 } else {
3413 tp_features.hotkey_mask = 1;
3414 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003415 break;
3416
3417 case 2:
3418 /*
3419 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3420 */
3421
3422 /* Paranoia check AND init hotkey_all_mask */
3423 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3424 "MHKA", "dd", 1)) {
3425 pr_err("missing MHKA handler, please report this to %s\n",
3426 TPACPI_MAIL);
3427 /* Fallback: pre-init for FN+F3,F4,F12 */
3428 hotkey_all_mask = 0x080cU;
3429 } else {
3430 tp_features.hotkey_mask = 1;
3431 }
3432
3433 /*
3434 * Check if we have an adaptive keyboard, like on the
3435 * Lenovo Carbon X1 2014 (2nd Gen).
3436 */
3437 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3438 "MHKA", "dd", 2)) {
Len Baker79f960e2021-10-23 17:40:36 +02003439 if (hotkey_adaptive_all_mask != 0)
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003440 tp_features.has_adaptive_kbd = true;
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003441 } else {
3442 tp_features.has_adaptive_kbd = false;
3443 hotkey_adaptive_all_mask = 0x0U;
3444 }
3445 break;
3446
3447 default:
3448 pr_err("unknown version of the HKEY interface: 0x%x\n",
3449 hkeyv);
3450 pr_err("please report this to %s\n", TPACPI_MAIL);
3451 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003452 }
3453 }
3454
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003455 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3456 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003457 str_supported(tp_features.hotkey_mask));
3458
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003459 /* Init hotkey_all_mask if not initialized yet */
3460 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3461 (quirks & TPACPI_HK_Q_INIMASK))
3462 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003463
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003464 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003465 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003466 /* hotkey_source_mask *must* be zero for
3467 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003468 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003469 if (res)
Hans de Goedecb97f5f2021-11-21 20:11:28 +01003470 return res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003471
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003472 hotkey_orig_mask = hotkey_acpi_mask;
3473 } else {
3474 hotkey_orig_mask = hotkey_all_mask;
3475 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003476 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003477
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003478#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3479 if (dbg_wlswemul) {
3480 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003481 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003482 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003483 } else
3484#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003485 /* Not all thinkpads have a hardware radio switch */
3486 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3487 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003488 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003489 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003490 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003491 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003492
Len Bakerc99ca782021-09-26 13:19:08 +02003493 tabletsw_state = hotkey_init_tablet_mode();
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003494
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003495 /* Set up key map */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003496 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3497 ARRAY_SIZE(tpacpi_keymap_qtable));
3498 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3499 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3500 "using keymap number %lu\n", keymap_id);
3501
Fuqian Huang35b7c802019-07-04 00:30:00 +08003502 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3503 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3504 if (!hotkey_keycode_map) {
3505 pr_err("failed to allocate memory for key map\n");
Hans de Goedecb97f5f2021-11-21 20:11:28 +01003506 return -ENOMEM;
Fuqian Huang35b7c802019-07-04 00:30:00 +08003507 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003508
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003509 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003510 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3511 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3512 tpacpi_inputdev->keycode = hotkey_keycode_map;
3513 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3514 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003515 input_set_capability(tpacpi_inputdev, EV_KEY,
3516 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003517 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003518 if (i < sizeof(hotkey_reserved_mask)*8)
3519 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003520 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003521 }
3522
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003523 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003524 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003525 input_report_switch(tpacpi_inputdev,
3526 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003527 }
3528 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003529 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003530 input_report_switch(tpacpi_inputdev,
3531 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003532 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003533
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003534 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003535 * userspace. tpacpi_detect_brightness_capabilities() must have
3536 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003537 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003538 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3539 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003540
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003541 /* Disable brightness up/down on Lenovo thinkpads when
3542 * ACPI is handling them, otherwise it is plain impossible
3543 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003544 hotkey_reserved_mask |=
3545 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3546 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003547 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3548 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003549 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003550
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003551#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003552 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3553 & ~hotkey_all_mask
3554 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003555
3556 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3557 "hotkey source mask 0x%08x, polling freq %u\n",
3558 hotkey_source_mask, hotkey_poll_freq);
3559#endif
3560
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003561 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3562 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003563 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003564 if (res) {
3565 hotkey_exit();
3566 return res;
3567 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003568 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3569 | hotkey_driver_mask)
3570 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003571 if (res < 0 && res != -ENXIO) {
3572 hotkey_exit();
3573 return res;
3574 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003575 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3576 & ~hotkey_reserved_mask;
3577 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3578 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3579 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003580
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003581 tpacpi_inputdev->open = &hotkey_inputdev_open;
3582 tpacpi_inputdev->close = &hotkey_inputdev_close;
3583
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003584 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003585
3586 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003587}
3588
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003589/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3590 * mode, Web conference mode, Function mode and Lay-flat mode.
3591 * We support Home mode and Function mode currently.
3592 *
3593 * Will consider support rest of modes in future.
3594 *
3595 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003596static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003597 HOME_MODE,
3598/* WEB_BROWSER_MODE = 2,
3599 WEB_CONFERENCE_MODE = 3, */
3600 FUNCTION_MODE
3601};
3602
3603#define DFR_CHANGE_ROW 0x101
3604#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003605#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003606
3607/* press Fn key a while second, it will switch to Function Mode. Then
3608 * release Fn key, previous mode be restored.
3609 */
3610static bool adaptive_keyboard_mode_is_saved;
3611static int adaptive_keyboard_prev_mode;
3612
Bastien Noceraf74587f2015-03-02 14:45:22 +01003613static int adaptive_keyboard_get_mode(void)
3614{
3615 int mode = 0;
3616
3617 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3618 pr_err("Cannot read adaptive keyboard mode\n");
3619 return -EIO;
3620 }
3621
3622 return mode;
3623}
3624
3625static int adaptive_keyboard_set_mode(int new_mode)
3626{
3627 if (new_mode < 0 ||
3628 new_mode > LAYFLAT_MODE)
3629 return -EINVAL;
3630
3631 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3632 pr_err("Cannot set adaptive keyboard mode\n");
3633 return -EIO;
3634 }
3635
3636 return 0;
3637}
3638
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003639static int adaptive_keyboard_get_next_mode(int mode)
3640{
3641 size_t i;
3642 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3643
3644 for (i = 0; i <= max_mode; i++) {
3645 if (adaptive_keyboard_modes[i] == mode)
3646 break;
3647 }
3648
3649 if (i >= max_mode)
3650 i = 0;
3651 else
3652 i++;
3653
3654 return adaptive_keyboard_modes[i];
3655}
3656
3657static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3658{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003659 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003660 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003661 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003662
3663 switch (scancode) {
3664 case DFR_CHANGE_ROW:
3665 if (adaptive_keyboard_mode_is_saved) {
3666 new_mode = adaptive_keyboard_prev_mode;
3667 adaptive_keyboard_mode_is_saved = false;
3668 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003669 current_mode = adaptive_keyboard_get_mode();
3670 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003671 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003672 new_mode = adaptive_keyboard_get_next_mode(
3673 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003674 }
3675
Bastien Noceraf74587f2015-03-02 14:45:22 +01003676 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003677 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003678
3679 return true;
3680
3681 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003682 current_mode = adaptive_keyboard_get_mode();
3683 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003684 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003685
Bastien Noceraf74587f2015-03-02 14:45:22 +01003686 adaptive_keyboard_prev_mode = current_mode;
3687 adaptive_keyboard_mode_is_saved = true;
3688
3689 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3690 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003691 return true;
3692
3693 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003694 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003695 scancode >= FIRST_ADAPTIVE_KEY +
3696 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3697 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003698 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003699 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003700 return false;
3701 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003702 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3703 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003704 if (keycode != KEY_RESERVED) {
3705 mutex_lock(&tpacpi_inputdev_send_mutex);
3706
3707 input_report_key(tpacpi_inputdev, keycode, 1);
3708 input_sync(tpacpi_inputdev);
3709
3710 input_report_key(tpacpi_inputdev, keycode, 0);
3711 input_sync(tpacpi_inputdev);
3712
3713 mutex_unlock(&tpacpi_inputdev_send_mutex);
3714 }
3715 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003716 }
3717}
3718
Hans de Goede1b8101d2021-10-05 22:23:18 +02003719static bool hotkey_notify_extended_hotkey(const u32 hkey)
3720{
3721 unsigned int scancode;
3722
Hans de Goedef8098912021-10-05 22:23:20 +02003723 switch (hkey) {
3724 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
3725 tpacpi_driver_event(hkey);
3726 return true;
3727 }
3728
Hans de Goede1b8101d2021-10-05 22:23:18 +02003729 /* Extended keycodes start at 0x300 and our offset into the map
3730 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3731 * will be positive, but might not be in the correct range.
3732 */
3733 scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3734 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3735 scancode < TPACPI_HOTKEY_MAP_LEN) {
3736 tpacpi_input_send_key(scancode);
3737 return true;
3738 }
3739
3740 return false;
3741}
3742
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003743static bool hotkey_notify_hotkey(const u32 hkey,
3744 bool *send_acpi_ev,
3745 bool *ignore_acpi_ev)
3746{
3747 /* 0x1000-0x1FFF: key presses */
3748 unsigned int scancode = hkey & 0xfff;
3749 *send_acpi_ev = true;
3750 *ignore_acpi_ev = false;
3751
Christian Kellner696c65232017-02-28 17:10:57 +01003752 /*
3753 * Original events are in the 0x10XX range, the adaptive keyboard
3754 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3755 * models, additional keys are emitted through 0x13XX.
3756 */
3757 switch ((hkey >> 8) & 0xf) {
3758 case 0:
3759 if (scancode > 0 &&
3760 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3761 /* HKEY event 0x1001 is scancode 0x00 */
3762 scancode--;
3763 if (!(hotkey_source_mask & (1 << scancode))) {
3764 tpacpi_input_send_key_masked(scancode);
3765 *send_acpi_ev = false;
3766 } else {
3767 *ignore_acpi_ev = true;
3768 }
3769 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003770 }
Christian Kellner696c65232017-02-28 17:10:57 +01003771 break;
3772
3773 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003774 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003775
3776 case 3:
Hans de Goede1b8101d2021-10-05 22:23:18 +02003777 return hotkey_notify_extended_hotkey(hkey);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003778 }
Christian Kellner696c65232017-02-28 17:10:57 +01003779
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003780 return false;
3781}
3782
3783static bool hotkey_notify_wakeup(const u32 hkey,
3784 bool *send_acpi_ev,
3785 bool *ignore_acpi_ev)
3786{
3787 /* 0x2000-0x2FFF: Wakeup reason */
3788 *send_acpi_ev = true;
3789 *ignore_acpi_ev = false;
3790
3791 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003792 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3793 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003794 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3795 *ignore_acpi_ev = true;
3796 break;
3797
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003798 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3799 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003800 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3801 *ignore_acpi_ev = true;
3802 break;
3803
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003804 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3805 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003806 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003807 /* how to auto-heal: */
3808 /* 2313: woke up from S3, go to S4/S5 */
3809 /* 2413: woke up from S4, go to S5 */
3810 break;
3811
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003812 default:
3813 return false;
3814 }
3815
3816 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003817 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003818 hotkey_wakeup_reason_notify_change();
3819 }
3820 return true;
3821}
3822
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003823static bool hotkey_notify_dockevent(const u32 hkey,
3824 bool *send_acpi_ev,
3825 bool *ignore_acpi_ev)
3826{
3827 /* 0x4000-0x4FFF: dock-related events */
3828 *send_acpi_ev = true;
3829 *ignore_acpi_ev = false;
3830
3831 switch (hkey) {
3832 case TP_HKEY_EV_UNDOCK_ACK:
3833 /* ACPI undock operation completed after wakeup */
3834 hotkey_autosleep_ack = 1;
3835 pr_info("undocked\n");
3836 hotkey_wakeup_hotunplug_complete_notify_change();
3837 return true;
3838
3839 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3840 pr_info("docked into hotplug port replicator\n");
3841 return true;
3842 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3843 pr_info("undocked from hotplug port replicator\n");
3844 return true;
3845
Alexander Kobel0a053f02021-02-13 16:13:36 +01003846 /*
3847 * Deliberately ignore attaching and detaching the keybord cover to avoid
3848 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3849 * to userspace.
3850 *
3851 * Please refer to the following thread for more information and a preliminary
3852 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3853 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3854 * the Pico cartridge dock module:
3855 * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
3856 */
3857 case TP_HKEY_EV_KBD_COVER_ATTACH:
3858 case TP_HKEY_EV_KBD_COVER_DETACH:
3859 *send_acpi_ev = false;
3860 *ignore_acpi_ev = true;
3861 return true;
3862
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003863 default:
3864 return false;
3865 }
3866}
3867
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003868static bool hotkey_notify_usrevent(const u32 hkey,
3869 bool *send_acpi_ev,
3870 bool *ignore_acpi_ev)
3871{
3872 /* 0x5000-0x5FFF: human interface helpers */
3873 *send_acpi_ev = true;
3874 *ignore_acpi_ev = false;
3875
3876 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003877 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3878 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003879 return true;
3880
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003881 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3882 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003883 tpacpi_input_send_tabletsw();
3884 hotkey_tablet_mode_notify_change();
3885 *send_acpi_ev = false;
3886 return true;
3887
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003888 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3889 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3890 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003891 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003892 *ignore_acpi_ev = true;
3893 return true;
3894
3895 default:
3896 return false;
3897 }
3898}
3899
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003900static void thermal_dump_all_sensors(void);
Mark Pearson1ac09652020-11-24 13:11:54 -05003901static void palmsensor_refresh(void);
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003902
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003903static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003904 bool *send_acpi_ev,
3905 bool *ignore_acpi_ev)
3906{
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003907 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003908 *send_acpi_ev = true;
3909 *ignore_acpi_ev = false;
3910
3911 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003912 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -03003913 pr_debug("EC reports: Thermal Table has changed\n");
3914 /* recommended action: do nothing, we don't have
3915 * Lenovo ATM information */
3916 return true;
3917 case TP_HKEY_EV_THM_CSM_COMPLETED:
3918 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04003919 /* Thermal event - pass on to event handler */
3920 tpacpi_driver_event(hkey);
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -03003921 return true;
3922 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3923 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003924 /* recommended action: do nothing, we don't have
3925 * Lenovo ATM information */
3926 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003927 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003928 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003929 /* recommended action: warn user through gui */
3930 break;
3931 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003932 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003933 /* recommended action: immediate sleep/hibernate */
3934 break;
3935 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003936 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003937 /* recommended action: warn user through gui, that */
3938 /* some internal component is too hot */
3939 break;
3940 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003941 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003942 /* recommended action: immediate sleep/hibernate */
3943 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003944 case TP_HKEY_EV_AC_CHANGED:
3945 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3946 * AC status changed; can be triggered by plugging or
3947 * unplugging AC adapter, docking or undocking. */
3948
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003949 fallthrough;
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003950
3951 case TP_HKEY_EV_KEY_NUMLOCK:
3952 case TP_HKEY_EV_KEY_FN:
3953 /* key press events, we just ignore them as long as the EC
3954 * is still reporting them in the normal keyboard stream */
3955 *send_acpi_ev = false;
3956 *ignore_acpi_ev = true;
3957 return true;
3958
Esteve Varela Colominas12dde4d2021-03-15 20:58:24 +01003959 case TP_HKEY_EV_KEY_FN_ESC:
Esteve Varela Colominas49702ba2021-03-21 19:35:13 +01003960 /* Get the media key status to force the status LED to update */
Esteve Varela Colominas12dde4d2021-03-15 20:58:24 +01003961 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3962 *send_acpi_ev = false;
3963 *ignore_acpi_ev = true;
3964 return true;
3965
Lyudeb03f4d42016-11-11 15:15:03 -05003966 case TP_HKEY_EV_TABLET_CHANGED:
3967 tpacpi_input_send_tabletsw();
3968 hotkey_tablet_mode_notify_change();
3969 *send_acpi_ev = false;
Henrique de Moraes Holschuhedd1ed72018-04-24 16:56:04 -03003970 return true;
Lyudeb03f4d42016-11-11 15:15:03 -05003971
David Herrmann587d8622018-01-12 12:04:45 +01003972 case TP_HKEY_EV_PALM_DETECTED:
3973 case TP_HKEY_EV_PALM_UNDETECTED:
Mark Pearson1ac09652020-11-24 13:11:54 -05003974 /* palm detected - pass on to event handler */
3975 palmsensor_refresh();
David Herrmann587d8622018-01-12 12:04:45 +01003976 return true;
3977
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003978 default:
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03003979 /* report simply as unknown, no sensor dump */
3980 return false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003981 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003982
3983 thermal_dump_all_sensors();
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03003984 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003985}
3986
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003987static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3988{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003989 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003990 bool send_acpi_ev;
3991 bool ignore_acpi_ev;
3992 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003993
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003994 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003995 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003996 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003997 acpi_bus_generate_netlink_event(
3998 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003999 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004000 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004001 return;
4002 }
4003
4004 while (1) {
4005 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004006 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004007 return;
4008 }
4009
4010 if (hkey == 0) {
4011 /* queue empty */
4012 return;
4013 }
4014
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004015 send_acpi_ev = true;
4016 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004017
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004018 switch (hkey >> 12) {
4019 case 1:
4020 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004021 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4022 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004023 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004024 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004025 /* 0x2000-0x2FFF: Wakeup reason */
4026 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4027 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004028 break;
4029 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004030 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004031 switch (hkey) {
4032 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004033 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004034 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004035 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004036 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004037 break;
4038 case TP_HKEY_EV_OPTDRV_EJ:
4039 /* FIXME: kick libata if SATA link offline */
4040 known_ev = true;
4041 break;
4042 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004043 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004044 }
4045 break;
4046 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004047 /* 0x4000-0x4FFF: dock-related events */
4048 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4049 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004050 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004051 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004052 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004053 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4054 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004055 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004056 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004057 /* 0x6000-0x6FFF: thermal alarms/notices and
4058 * keyboard events */
4059 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004060 &ignore_acpi_ev);
4061 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004062 case 7:
4063 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004064 if (tp_features.hotkey_wlsw &&
4065 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004066 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004067 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004068 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004069 break;
4070 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004071 fallthrough; /* to default */
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004072 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004073 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004074 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004075 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004076 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004077 pr_notice("please report the conditions when this event happened to %s\n",
4078 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004079 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004080
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004081 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004082 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004083 acpi_bus_generate_netlink_event(
4084 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004085 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004086 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004087 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004088 }
4089}
4090
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004091static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004092{
4093 /* Do these on suspend, we get the events on early resume! */
4094 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4095 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004096
4097 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004098 if (tp_features.has_adaptive_kbd) {
4099 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4100 "GTRW", "dd", 0)) {
4101 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004102 }
4103 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004104}
4105
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004106static void hotkey_resume(void)
4107{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004108 tpacpi_disable_brightness_delay();
4109
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004110 if (hotkey_status_set(true) < 0 ||
4111 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004112 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004113
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004114 tpacpi_send_radiosw_update();
Benjamin Berg96adb412020-11-23 14:21:57 +01004115 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004116 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004117 hotkey_wakeup_reason_notify_change();
4118 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004119 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004120
4121 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004122 if (tp_features.has_adaptive_kbd) {
4123 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4124 adaptive_keyboard_prev_mode)) {
4125 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004126 }
4127 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004128}
4129
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004130/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004131static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004133 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004135 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004136 seq_printf(m, "status:\t\tnot supported\n");
4137 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004138 }
4139
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004140 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004141 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004142 res = hotkey_status_get(&status);
4143 if (!res)
4144 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004145 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004146 if (res)
4147 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004149 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004150 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004151 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4152 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004154 seq_printf(m, "mask:\t\tnot supported\n");
4155 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 }
4157
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004161static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004162{
4163 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004164 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004165 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4166 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 +00004167}
4168
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004169static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004171 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004172 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004175 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return -ENODEV;
4177
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004178 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004179 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004180
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004181 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004182
4183 res = 0;
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03004184 while ((cmd = strsep(&buf, ","))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004186 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004188 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004189 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004191 mask = (hotkey_all_mask | hotkey_source_mask)
4192 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4194 /* mask set */
4195 } else if (sscanf(cmd, "%x", &mask) == 1) {
4196 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004197 } else {
4198 res = -EINVAL;
4199 goto errexit;
4200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004202
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004203 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004204 tpacpi_disclose_usertask("procfs hotkey",
4205 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004206 res = hotkey_user_mask_set(mask);
4207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004209errexit:
4210 mutex_unlock(&hotkey_mutex);
4211 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004212}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004214static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004215 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4216 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004217 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004218 {"", 0},
4219};
4220
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004221static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004222 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004223 .notify = hotkey_notify,
4224 .handle = &hkey_handle,
4225 .type = ACPI_DEVICE_NOTIFY,
4226};
4227
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004228static struct ibm_struct hotkey_driver_data = {
4229 .name = "hotkey",
4230 .read = hotkey_read,
4231 .write = hotkey_write,
4232 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004233 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004234 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004235 .acpi = &ibm_hotkey_acpidriver,
4236};
4237
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004238/*************************************************************************
4239 * Bluetooth subdriver
4240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004242enum {
4243 /* ACPI GBDC/SBDC bits */
4244 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4245 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004246 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004247 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004248};
4249
4250enum {
4251 /* ACPI \BLTH commands */
4252 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4253 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4254 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4255 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4256 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004257};
4258
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004259#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4260
Johannes Berg19d337d2009-06-02 13:01:37 +02004261static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004262{
4263 int status;
4264
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004265#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4266 if (dbg_bluetoothemul)
4267 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004268 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004269#endif
4270
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004271 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4272 return -EIO;
4273
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004274 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004275 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004276}
4277
Johannes Berg19d337d2009-06-02 13:01:37 +02004278static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004279{
4280 int status;
4281
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004282 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004283 "will attempt to %s bluetooth\n",
4284 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004285
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004286#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4287 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004288 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004289 return 0;
4290 }
4291#endif
4292
Johannes Berg19d337d2009-06-02 13:01:37 +02004293 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004294 status = TP_ACPI_BLUETOOTH_RADIOSSW
4295 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4296 else
4297 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004298
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004299 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4300 return -EIO;
4301
4302 return 0;
4303}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004304
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004305/* sysfs bluetooth enable ---------------------------------------------- */
4306static ssize_t bluetooth_enable_show(struct device *dev,
4307 struct device_attribute *attr,
4308 char *buf)
4309{
Johannes Berg19d337d2009-06-02 13:01:37 +02004310 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4311 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004312}
4313
4314static ssize_t bluetooth_enable_store(struct device *dev,
4315 struct device_attribute *attr,
4316 const char *buf, size_t count)
4317{
Johannes Berg19d337d2009-06-02 13:01:37 +02004318 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4319 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004320}
4321
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004322static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004323
4324/* --------------------------------------------------------------------- */
4325
4326static struct attribute *bluetooth_attributes[] = {
4327 &dev_attr_bluetooth_enable.attr,
4328 NULL
4329};
4330
Len Baker79f960e2021-10-23 17:40:36 +02004331static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
4332 struct attribute *attr, int n)
4333{
4334 return tp_features.bluetooth ? attr->mode : 0;
4335}
4336
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004337static const struct attribute_group bluetooth_attr_group = {
Len Baker79f960e2021-10-23 17:40:36 +02004338 .is_visible = bluetooth_attr_is_visible,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004339 .attrs = bluetooth_attributes,
4340};
4341
Johannes Berg19d337d2009-06-02 13:01:37 +02004342static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4343 .get_status = bluetooth_get_status,
4344 .set_status = bluetooth_set_status,
4345};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004346
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004347static void bluetooth_shutdown(void)
4348{
4349 /* Order firmware to save current state to NVRAM */
4350 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4351 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004352 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004353 else
4354 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004355 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004356}
4357
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004358static void bluetooth_exit(void)
4359{
Johannes Berg19d337d2009-06-02 13:01:37 +02004360 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Johannes Berg19d337d2009-06-02 13:01:37 +02004361 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004362}
4363
Jiaxun Yangf7db8392019-03-07 17:37:16 +08004364static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4365 {
4366 .ident = "ThinkPad E485",
4367 .matches = {
4368 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4369 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4370 },
4371 },
4372 {
4373 .ident = "ThinkPad E585",
4374 .matches = {
4375 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4376 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4377 },
4378 },
4379 {
4380 .ident = "ThinkPad A285 - 20MW",
4381 .matches = {
4382 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4383 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4384 },
4385 },
4386 {
4387 .ident = "ThinkPad A285 - 20MX",
4388 .matches = {
4389 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4390 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4391 },
4392 },
4393 {
4394 .ident = "ThinkPad A485 - 20MU",
4395 .matches = {
4396 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4397 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4398 },
4399 },
4400 {
4401 .ident = "ThinkPad A485 - 20MV",
4402 .matches = {
4403 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4404 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4405 },
4406 },
4407 {}
4408};
4409
4410static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4411 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4412 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4413 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4414 {}
4415};
4416
4417
4418static int __init have_bt_fwbug(void)
4419{
4420 /*
4421 * Some AMD based ThinkPads have a firmware bug that calling
4422 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4423 */
4424 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4425 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4426 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4427 return 1;
4428 } else
4429 return 0;
4430}
4431
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004432static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004434 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004435 int status = 0;
4436
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004437 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4438 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004439
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004440 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004441
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004442 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4443 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Jiaxun Yangf7db8392019-03-07 17:37:16 +08004444 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004445 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004447 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4448 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004449 str_supported(tp_features.bluetooth),
4450 status);
4451
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004452#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4453 if (dbg_bluetoothemul) {
4454 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004455 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004456 } else
4457#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004458 if (tp_features.bluetooth &&
4459 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4460 /* no bluetooth hardware present in system */
4461 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004462 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004463 "bluetooth hardware not installed\n");
4464 }
4465
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004466 if (!tp_features.bluetooth)
Hans de Goedec7e1c782021-11-21 20:11:24 +01004467 return -ENODEV;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004468
Johannes Berg19d337d2009-06-02 13:01:37 +02004469 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4470 &bluetooth_tprfk_ops,
4471 RFKILL_TYPE_BLUETOOTH,
4472 TPACPI_RFK_BLUETOOTH_SW_NAME,
4473 true);
Len Baker79f960e2021-10-23 17:40:36 +02004474 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004477/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004478static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004480 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481}
4482
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004483static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484{
Johannes Berg19d337d2009-06-02 13:01:37 +02004485 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486}
4487
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004488static struct ibm_struct bluetooth_driver_data = {
4489 .name = "bluetooth",
4490 .read = bluetooth_read,
4491 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004492 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004493 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004494};
4495
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004496/*************************************************************************
4497 * Wan subdriver
4498 */
4499
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004500enum {
4501 /* ACPI GWAN/SWAN bits */
4502 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4503 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004504 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004505 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004506};
4507
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004508#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4509
Johannes Berg19d337d2009-06-02 13:01:37 +02004510static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004511{
4512 int status;
4513
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004514#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4515 if (dbg_wwanemul)
4516 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004517 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004518#endif
4519
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004520 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4521 return -EIO;
4522
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004523 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004524 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004525}
4526
Johannes Berg19d337d2009-06-02 13:01:37 +02004527static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004528{
4529 int status;
4530
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004531 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004532 "will attempt to %s wwan\n",
4533 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004534
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004535#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4536 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004537 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004538 return 0;
4539 }
4540#endif
4541
Johannes Berg19d337d2009-06-02 13:01:37 +02004542 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004543 status = TP_ACPI_WANCARD_RADIOSSW
4544 | TP_ACPI_WANCARD_RESUMECTRL;
4545 else
4546 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004547
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004548 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4549 return -EIO;
4550
4551 return 0;
4552}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004553
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004554/* sysfs wan enable ---------------------------------------------------- */
4555static ssize_t wan_enable_show(struct device *dev,
4556 struct device_attribute *attr,
4557 char *buf)
4558{
Johannes Berg19d337d2009-06-02 13:01:37 +02004559 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4560 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004561}
4562
4563static ssize_t wan_enable_store(struct device *dev,
4564 struct device_attribute *attr,
4565 const char *buf, size_t count)
4566{
Johannes Berg19d337d2009-06-02 13:01:37 +02004567 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4568 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004569}
4570
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004571static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4572 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004573
4574/* --------------------------------------------------------------------- */
4575
4576static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004577 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004578 NULL
4579};
4580
Len Baker79f960e2021-10-23 17:40:36 +02004581static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
4582 int n)
4583{
4584 return tp_features.wan ? attr->mode : 0;
4585}
4586
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004587static const struct attribute_group wan_attr_group = {
Len Baker79f960e2021-10-23 17:40:36 +02004588 .is_visible = wan_attr_is_visible,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004589 .attrs = wan_attributes,
4590};
4591
Johannes Berg19d337d2009-06-02 13:01:37 +02004592static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4593 .get_status = wan_get_status,
4594 .set_status = wan_set_status,
4595};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004596
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004597static void wan_shutdown(void)
4598{
4599 /* Order firmware to save current state to NVRAM */
4600 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4601 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004602 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004603 else
4604 vdbg_printk(TPACPI_DBG_RFKILL,
4605 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004606}
4607
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004608static void wan_exit(void)
4609{
Johannes Berg19d337d2009-06-02 13:01:37 +02004610 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Johannes Berg19d337d2009-06-02 13:01:37 +02004611 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004612}
4613
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004614static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004615{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004616 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004617 int status = 0;
4618
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004619 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4620 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004621
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004622 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004623
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004624 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004625 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004626
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004627 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4628 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004629 str_supported(tp_features.wan),
4630 status);
4631
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004632#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4633 if (dbg_wwanemul) {
4634 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004635 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004636 } else
4637#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004638 if (tp_features.wan &&
4639 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4640 /* no wan hardware present in system */
4641 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004642 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004643 "wan hardware not installed\n");
4644 }
4645
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004646 if (!tp_features.wan)
Hans de Goedec7e1c782021-11-21 20:11:24 +01004647 return -ENODEV;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004648
Johannes Berg19d337d2009-06-02 13:01:37 +02004649 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4650 &wan_tprfk_ops,
4651 RFKILL_TYPE_WWAN,
4652 TPACPI_RFK_WWAN_SW_NAME,
4653 true);
Len Baker79f960e2021-10-23 17:40:36 +02004654 return res;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004655}
4656
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004657/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004658static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004659{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004660 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004661}
4662
4663static int wan_write(char *buf)
4664{
Johannes Berg19d337d2009-06-02 13:01:37 +02004665 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004666}
4667
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004668static struct ibm_struct wan_driver_data = {
4669 .name = "wan",
4670 .read = wan_read,
4671 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004672 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004673 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004674};
4675
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004676/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004677 * UWB subdriver
4678 */
4679
4680enum {
4681 /* ACPI GUWB/SUWB bits */
4682 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4683 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4684};
4685
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004686#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4687
Johannes Berg19d337d2009-06-02 13:01:37 +02004688static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004689{
4690 int status;
4691
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004692#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4693 if (dbg_uwbemul)
4694 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004695 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004696#endif
4697
4698 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4699 return -EIO;
4700
4701 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004702 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004703}
4704
Johannes Berg19d337d2009-06-02 13:01:37 +02004705static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004706{
4707 int status;
4708
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004709 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004710 "will attempt to %s UWB\n",
4711 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004712
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004713#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4714 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004715 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004716 return 0;
4717 }
4718#endif
4719
Johannes Berg19d337d2009-06-02 13:01:37 +02004720 if (state == TPACPI_RFK_RADIO_ON)
4721 status = TP_ACPI_UWB_RADIOSSW;
4722 else
4723 status = 0;
4724
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004725 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4726 return -EIO;
4727
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004728 return 0;
4729}
4730
4731/* --------------------------------------------------------------------- */
4732
Johannes Berg19d337d2009-06-02 13:01:37 +02004733static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4734 .get_status = uwb_get_status,
4735 .set_status = uwb_set_status,
4736};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004737
4738static void uwb_exit(void)
4739{
Johannes Berg19d337d2009-06-02 13:01:37 +02004740 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004741}
4742
4743static int __init uwb_init(struct ibm_init_struct *iibm)
4744{
4745 int res;
4746 int status = 0;
4747
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004748 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4749 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004750
4751 TPACPI_ACPIHANDLE_INIT(hkey);
4752
4753 tp_features.uwb = hkey_handle &&
4754 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4755
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004756 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4757 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004758 str_supported(tp_features.uwb),
4759 status);
4760
4761#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4762 if (dbg_uwbemul) {
4763 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004764 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004765 } else
4766#endif
4767 if (tp_features.uwb &&
4768 !(status & TP_ACPI_UWB_HWPRESENT)) {
4769 /* no uwb hardware present in system */
4770 tp_features.uwb = 0;
4771 dbg_printk(TPACPI_DBG_INIT,
4772 "uwb hardware not installed\n");
4773 }
4774
4775 if (!tp_features.uwb)
Hans de Goedec7e1c782021-11-21 20:11:24 +01004776 return -ENODEV;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004777
4778 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004779 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004780 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004781 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004782 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004783 return res;
4784}
4785
4786static struct ibm_struct uwb_driver_data = {
4787 .name = "uwb",
4788 .exit = uwb_exit,
4789 .flags.experimental = 1,
4790};
4791
4792/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004793 * Video subdriver
4794 */
4795
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004796#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4797
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004798enum video_access_mode {
4799 TPACPI_VIDEO_NONE = 0,
4800 TPACPI_VIDEO_570, /* 570 */
4801 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4802 TPACPI_VIDEO_NEW, /* all others */
4803};
4804
4805enum { /* video status flags, based on VIDEO_570 */
4806 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4807 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4808 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4809};
4810
4811enum { /* TPACPI_VIDEO_570 constants */
4812 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4813 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4814 * video_status_flags */
4815 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4816 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4817};
4818
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004819static enum video_access_mode video_supported;
4820static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004821
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004822static int video_autosw_get(void);
4823static int video_autosw_set(int enable);
4824
Joe Perches112a6ee2011-04-04 10:06:24 -07004825TPACPI_HANDLE(vid, root,
4826 "\\_SB.PCI.AGP.VGA", /* 570 */
4827 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4828 "\\_SB.PCI0.VID0", /* 770e */
4829 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4830 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4831 "\\_SB.PCI0.AGP.VID", /* all others */
4832 ); /* R30, R31 */
4833
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004834TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004835
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004836static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004838 int ivga;
4839
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004840 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4841
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004842 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004843 if (tpacpi_is_ibm())
4844 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004845
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004846 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4847 /* G41, assume IVGA doesn't change */
4848 vid_handle = vid2_handle;
4849
4850 if (!vid_handle)
4851 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004852 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004853 else if (tpacpi_is_ibm() &&
4854 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004855 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004856 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004857 else if (tpacpi_is_ibm() &&
4858 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004859 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004860 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004861 else
4862 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004863 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004865 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4866 str_supported(video_supported != TPACPI_VIDEO_NONE),
4867 video_supported);
4868
Hans de Goedec7e1c782021-11-21 20:11:24 +01004869 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870}
4871
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004872static void video_exit(void)
4873{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004874 dbg_printk(TPACPI_DBG_EXIT,
4875 "restoring original video autoswitch mode\n");
4876 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004877 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004878}
4879
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004880static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881{
4882 int status = 0;
4883 int i;
4884
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004885 switch (video_supported) {
4886 case TPACPI_VIDEO_570:
4887 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4888 TP_ACPI_VIDEO_570_PHSCMD))
4889 return -EIO;
4890 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4891 break;
4892 case TPACPI_VIDEO_770:
4893 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4894 return -EIO;
4895 if (i)
4896 status |= TP_ACPI_VIDEO_S_LCD;
4897 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4898 return -EIO;
4899 if (i)
4900 status |= TP_ACPI_VIDEO_S_CRT;
4901 break;
4902 case TPACPI_VIDEO_NEW:
4903 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4904 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4905 return -EIO;
4906 if (i)
4907 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004909 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4910 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4911 return -EIO;
4912 if (i)
4913 status |= TP_ACPI_VIDEO_S_LCD;
4914 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4915 return -EIO;
4916 if (i)
4917 status |= TP_ACPI_VIDEO_S_DVI;
4918 break;
4919 default:
4920 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 return status;
4924}
4925
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004926static int video_outputsw_set(int status)
4927{
4928 int autosw;
4929 int res = 0;
4930
4931 switch (video_supported) {
4932 case TPACPI_VIDEO_570:
4933 res = acpi_evalf(NULL, NULL,
4934 "\\_SB.PHS2", "vdd",
4935 TP_ACPI_VIDEO_570_PHS2CMD,
4936 status | TP_ACPI_VIDEO_570_PHS2SET);
4937 break;
4938 case TPACPI_VIDEO_770:
4939 autosw = video_autosw_get();
4940 if (autosw < 0)
4941 return autosw;
4942
4943 res = video_autosw_set(1);
4944 if (res)
4945 return res;
4946 res = acpi_evalf(vid_handle, NULL,
4947 "ASWT", "vdd", status * 0x100, 0);
4948 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004949 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004950 return -EIO;
4951 }
4952 break;
4953 case TPACPI_VIDEO_NEW:
4954 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004955 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004956 break;
4957 default:
4958 return -ENOSYS;
4959 }
4960
Jan van den Berg72a979f2014-09-17 00:01:08 +02004961 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004962}
4963
4964static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966 int autosw = 0;
4967
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004968 switch (video_supported) {
4969 case TPACPI_VIDEO_570:
4970 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4971 return -EIO;
4972 break;
4973 case TPACPI_VIDEO_770:
4974 case TPACPI_VIDEO_NEW:
4975 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4976 return -EIO;
4977 break;
4978 default:
4979 return -ENOSYS;
4980 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004981
4982 return autosw & 1;
4983}
4984
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004985static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004986{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004987 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004988 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004989 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004990}
4991
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004992static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004993{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004994 int autosw = video_autosw_get();
4995 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004996
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004997 if (autosw < 0)
4998 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004999
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005000 switch (video_supported) {
5001 case TPACPI_VIDEO_570:
5002 res = video_autosw_set(1);
5003 if (res)
5004 return res;
5005 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5006 break;
5007 case TPACPI_VIDEO_770:
5008 case TPACPI_VIDEO_NEW:
5009 res = video_autosw_set(1);
5010 if (res)
5011 return res;
5012 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5013 break;
5014 default:
5015 return -ENOSYS;
5016 }
5017 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005018 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005019 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005020 }
5021
Jan van den Berg72a979f2014-09-17 00:01:08 +02005022 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005023}
5024
5025static int video_expand_toggle(void)
5026{
5027 switch (video_supported) {
5028 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005029 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005030 0 : -EIO;
5031 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005032 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005033 0 : -EIO;
5034 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005035 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005036 0 : -EIO;
5037 default:
5038 return -ENOSYS;
5039 }
5040 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005041}
5042
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005043static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005044{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005045 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005046
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005047 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005048 seq_printf(m, "status:\t\tnot supported\n");
5049 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005050 }
5051
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005052 /* Even reads can crash X.org, so... */
5053 if (!capable(CAP_SYS_ADMIN))
5054 return -EPERM;
5055
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005056 status = video_outputsw_get();
5057 if (status < 0)
5058 return status;
5059
5060 autosw = video_autosw_get();
5061 if (autosw < 0)
5062 return autosw;
5063
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005064 seq_printf(m, "status:\t\tsupported\n");
5065 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5066 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005067 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005068 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5069 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5070 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5071 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005072 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005073 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5074 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5075 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005076
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005077 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005078}
5079
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005080static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081{
5082 char *cmd;
5083 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005084 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005086 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005087 return -ENODEV;
5088
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005089 /* Even reads can crash X.org, let alone writes... */
5090 if (!capable(CAP_SYS_ADMIN))
5091 return -EPERM;
5092
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005093 enable = 0;
5094 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005096 while ((cmd = strsep(&buf, ","))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005098 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005100 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005102 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005104 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005105 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005106 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005107 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005108 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005109 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005110 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005112 res = video_autosw_set(1);
5113 if (res)
5114 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005116 res = video_autosw_set(0);
5117 if (res)
5118 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005120 res = video_outputsw_cycle();
5121 if (res)
5122 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005124 res = video_expand_toggle();
5125 if (res)
5126 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 } else
5128 return -EINVAL;
5129 }
5130
5131 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005132 status = video_outputsw_get();
5133 if (status < 0)
5134 return status;
5135 res = video_outputsw_set((status & ~disable) | enable);
5136 if (res)
5137 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 }
5139
5140 return 0;
5141}
5142
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005143static struct ibm_struct video_driver_data = {
5144 .name = "video",
5145 .read = video_read,
5146 .write = video_write,
5147 .exit = video_exit,
5148};
5149
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005150#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5151
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005152/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005153 * Keyboard backlight subdriver
5154 */
5155
Hans de Goedec685e202017-02-09 16:44:13 +01005156static enum led_brightness kbdlight_brightness;
5157static DEFINE_MUTEX(kbdlight_mutex);
5158
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005159static int kbdlight_set_level(int level)
5160{
Hans de Goedec685e202017-02-09 16:44:13 +01005161 int ret = 0;
5162
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005163 if (!hkey_handle)
5164 return -ENXIO;
5165
Hans de Goedec685e202017-02-09 16:44:13 +01005166 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005167
Hans de Goedec685e202017-02-09 16:44:13 +01005168 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5169 ret = -EIO;
5170 else
5171 kbdlight_brightness = level;
5172
5173 mutex_unlock(&kbdlight_mutex);
5174
5175 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005176}
5177
5178static int kbdlight_get_level(void)
5179{
5180 int status = 0;
5181
5182 if (!hkey_handle)
5183 return -ENXIO;
5184
5185 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5186 return -EIO;
5187
5188 if (status < 0)
5189 return status;
5190
5191 return status & 0x3;
5192}
5193
5194static bool kbdlight_is_supported(void)
5195{
5196 int status = 0;
5197
5198 if (!hkey_handle)
5199 return false;
5200
5201 if (!acpi_has_method(hkey_handle, "MLCG")) {
5202 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5203 return false;
5204 }
5205
5206 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5207 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5208 return false;
5209 }
5210
5211 if (status < 0) {
5212 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5213 return false;
5214 }
5215
5216 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5217 /*
5218 * Guessed test for keyboard backlight:
5219 *
5220 * Machines with backlight keyboard return:
5221 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5222 * b110100010010000000XX - ThinkPad x230
5223 * b010100000010000000XX - ThinkPad x240
5224 * b010100000010000000XX - ThinkPad W541
5225 * (XX is current backlight level)
5226 *
5227 * Machines without backlight keyboard return:
5228 * b10100001000000000000 - ThinkPad x230
5229 * b10110001000000000000 - ThinkPad E430
5230 * b00000000000000000000 - ThinkPad E450
5231 *
5232 * Candidate BITs for detection test (XOR):
5233 * b01000000001000000000
5234 * ^
5235 */
5236 return status & BIT(9);
5237}
5238
Hans de Goede86ec0c22017-02-09 16:44:12 +01005239static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005240 enum led_brightness brightness)
5241{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005242 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005243}
5244
5245static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5246{
5247 int level;
5248
5249 level = kbdlight_get_level();
5250 if (level < 0)
5251 return 0;
5252
5253 return level;
5254}
5255
5256static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5257 .led_classdev = {
5258 .name = "tpacpi::kbd_backlight",
5259 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005260 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005261 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005262 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005263 }
5264};
5265
5266static int __init kbdlight_init(struct ibm_init_struct *iibm)
5267{
5268 int rc;
5269
5270 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5271
5272 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005273
5274 if (!kbdlight_is_supported()) {
5275 tp_features.kbdlight = 0;
5276 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
Hans de Goedec7e1c782021-11-21 20:11:24 +01005277 return -ENODEV;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005278 }
5279
Hans de Goedec685e202017-02-09 16:44:13 +01005280 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005281 tp_features.kbdlight = 1;
5282
5283 rc = led_classdev_register(&tpacpi_pdev->dev,
5284 &tpacpi_led_kbdlight.led_classdev);
5285 if (rc < 0) {
5286 tp_features.kbdlight = 0;
5287 return rc;
5288 }
5289
Hans de Goedec685e202017-02-09 16:44:13 +01005290 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5291 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005292 return 0;
5293}
5294
5295static void kbdlight_exit(void)
5296{
Andy Shevchenko41f80042020-05-07 19:37:23 +03005297 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005298}
5299
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005300static int kbdlight_set_level_and_update(int level)
5301{
5302 int ret;
5303 struct led_classdev *led_cdev;
5304
5305 ret = kbdlight_set_level(level);
5306 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5307
5308 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5309 led_cdev->brightness = level;
5310
5311 return ret;
5312}
5313
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005314static int kbdlight_read(struct seq_file *m)
5315{
5316 int level;
5317
5318 if (!tp_features.kbdlight) {
5319 seq_printf(m, "status:\t\tnot supported\n");
5320 } else {
5321 level = kbdlight_get_level();
5322 if (level < 0)
5323 seq_printf(m, "status:\t\terror %d\n", level);
5324 else
5325 seq_printf(m, "status:\t\t%d\n", level);
5326 seq_printf(m, "commands:\t0, 1, 2\n");
5327 }
5328
5329 return 0;
5330}
5331
5332static int kbdlight_write(char *buf)
5333{
5334 char *cmd;
Andy Shevchenko466f4692020-05-11 15:54:14 +03005335 int res, level = -EINVAL;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005336
5337 if (!tp_features.kbdlight)
5338 return -ENODEV;
5339
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005340 while ((cmd = strsep(&buf, ","))) {
Andy Shevchenko466f4692020-05-11 15:54:14 +03005341 res = kstrtoint(cmd, 10, &level);
5342 if (res < 0)
5343 return res;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005344 }
5345
Andy Shevchenko466f4692020-05-11 15:54:14 +03005346 if (level >= 3 || level < 0)
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005347 return -EINVAL;
5348
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005349 return kbdlight_set_level_and_update(level);
5350}
5351
5352static void kbdlight_suspend(void)
5353{
5354 struct led_classdev *led_cdev;
5355
5356 if (!tp_features.kbdlight)
5357 return;
5358
5359 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5360 led_update_brightness(led_cdev);
5361 led_classdev_suspend(led_cdev);
5362}
5363
5364static void kbdlight_resume(void)
5365{
5366 if (!tp_features.kbdlight)
5367 return;
5368
5369 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005370}
5371
5372static struct ibm_struct kbdlight_driver_data = {
5373 .name = "kbdlight",
5374 .read = kbdlight_read,
5375 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005376 .suspend = kbdlight_suspend,
5377 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005378 .exit = kbdlight_exit,
5379};
5380
5381/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005382 * Light (thinklight) subdriver
5383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005385TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5386TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005387
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005388static int light_get_status(void)
5389{
5390 int status = 0;
5391
5392 if (tp_features.light_status) {
5393 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5394 return -EIO;
5395 return (!!status);
5396 }
5397
5398 return -ENXIO;
5399}
5400
5401static int light_set_status(int status)
5402{
5403 int rc;
5404
5405 if (tp_features.light) {
5406 if (cmos_handle) {
5407 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005408 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005409 TP_CMOS_THINKLIGHT_ON :
5410 TP_CMOS_THINKLIGHT_OFF);
5411 } else {
5412 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005413 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005414 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005415 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005416 }
5417
5418 return -ENXIO;
5419}
5420
Hans de Goede86ec0c22017-02-09 16:44:12 +01005421static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005422 enum led_brightness brightness)
5423{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005424 return light_set_status((brightness != LED_OFF) ?
5425 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005426}
5427
5428static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5429{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005430 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005431}
5432
5433static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5434 .led_classdev = {
5435 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005436 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005437 .brightness_get = &light_sysfs_get,
5438 }
5439};
5440
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005441static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005443 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005444
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005445 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5446
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005447 if (tpacpi_is_ibm()) {
5448 TPACPI_ACPIHANDLE_INIT(ledb);
5449 TPACPI_ACPIHANDLE_INIT(lght);
5450 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005451 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005452
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005453 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005454 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005455
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005456 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005457 /* light status not supported on
5458 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005459 tp_features.light_status =
5460 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005462 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5463 str_supported(tp_features.light),
5464 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005465
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005466 if (!tp_features.light)
Hans de Goedec7e1c782021-11-21 20:11:24 +01005467 return -ENODEV;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005468
5469 rc = led_classdev_register(&tpacpi_pdev->dev,
5470 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005471
5472 if (rc < 0) {
5473 tp_features.light = 0;
5474 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005475 } else {
5476 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005477 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005478
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005479 return rc;
5480}
5481
5482static void light_exit(void)
5483{
5484 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485}
5486
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005487static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005489 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005491 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005492 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005493 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005494 seq_printf(m, "status:\t\tunknown\n");
5495 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005496 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005497 status = light_get_status();
5498 if (status < 0)
5499 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005500 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5501 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505}
5506
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005507static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005510 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005511
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005512 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005513 return -ENODEV;
5514
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005515 while ((cmd = strsep(&buf, ","))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005517 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005519 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 } else
5521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 }
5523
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005524 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525}
5526
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005527static struct ibm_struct light_driver_data = {
5528 .name = "light",
5529 .read = light_read,
5530 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005531 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005532};
5533
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005534/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005535 * CMOS subdriver
5536 */
5537
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005538/* sysfs cmos_command -------------------------------------------------- */
5539static ssize_t cmos_command_store(struct device *dev,
5540 struct device_attribute *attr,
5541 const char *buf, size_t count)
5542{
5543 unsigned long cmos_cmd;
5544 int res;
5545
5546 if (parse_strtoul(buf, 21, &cmos_cmd))
5547 return -EINVAL;
5548
5549 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005550 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005551}
5552
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005553static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005554
Len Baker79f960e2021-10-23 17:40:36 +02005555static struct attribute *cmos_attributes[] = {
5556 &dev_attr_cmos_command.attr,
5557 NULL
5558};
5559
5560static umode_t cmos_attr_is_visible(struct kobject *kobj,
5561 struct attribute *attr, int n)
5562{
5563 return cmos_handle ? attr->mode : 0;
5564}
5565
5566static const struct attribute_group cmos_attr_group = {
5567 .is_visible = cmos_attr_is_visible,
5568 .attrs = cmos_attributes,
5569};
5570
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005571/* --------------------------------------------------------------------- */
5572
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005573static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005574{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005575 vdbg_printk(TPACPI_DBG_INIT,
Len Baker79f960e2021-10-23 17:40:36 +02005576 "initializing cmos commands subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005577
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005578 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005579
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005580 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
Len Baker79f960e2021-10-23 17:40:36 +02005581 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005582
Hans de Goedec7e1c782021-11-21 20:11:24 +01005583 return cmos_handle ? 0 : -ENODEV;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005584}
5585
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005586static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005588 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5589 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005591 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005593 seq_printf(m, "status:\t\tsupported\n");
5594 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 }
5596
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598}
5599
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005600static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
5602 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005603 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005605 while ((cmd = strsep(&buf, ","))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005606 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5607 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 /* cmos_cmd set */
5609 } else
5610 return -EINVAL;
5611
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005612 res = issue_thinkpad_cmos_command(cmos_cmd);
5613 if (res)
5614 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 }
5616
5617 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005618}
5619
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005620static struct ibm_struct cmos_driver_data = {
5621 .name = "cmos",
5622 .read = cmos_read,
5623 .write = cmos_write,
5624};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005625
5626/*************************************************************************
5627 * LED subdriver
5628 */
5629
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005630enum led_access_mode {
5631 TPACPI_LED_NONE = 0,
5632 TPACPI_LED_570, /* 570 */
5633 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5634 TPACPI_LED_NEW, /* all others */
5635};
5636
5637enum { /* For TPACPI_LED_OLD */
5638 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5639 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5640 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5641};
5642
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005643static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005644
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005645static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005646
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005647#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005648static struct tpacpi_led_classdev *tpacpi_leds;
5649static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005650static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005651 /* there's a limit of 19 chars + NULL before 2.6.26 */
5652 "tpacpi::power",
5653 "tpacpi:orange:batt",
5654 "tpacpi:green:batt",
5655 "tpacpi::dock_active",
5656 "tpacpi::bay_active",
5657 "tpacpi::dock_batt",
5658 "tpacpi::unknown_led",
5659 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005660 "tpacpi::dock_status1",
5661 "tpacpi::dock_status2",
Hans de Goedebe892e92021-11-23 22:05:24 +01005662 "tpacpi::lid_logo_dot",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005663 "tpacpi::unknown_led3",
5664 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005665};
Hans de Goedebe892e92021-11-23 22:05:24 +01005666#define TPACPI_SAFE_LEDS 0x1481U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005667
5668static inline bool tpacpi_is_led_restricted(const unsigned int led)
5669{
5670#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5671 return false;
5672#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005673 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005674#endif
5675}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005676
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005677static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005678{
5679 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005680 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005681
5682 switch (led_supported) {
5683 case TPACPI_LED_570:
5684 if (!acpi_evalf(ec_handle,
5685 &status, "GLED", "dd", 1 << led))
5686 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005687 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005688 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005689 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005690 TPACPI_LED_ON :
5691 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005692 tpacpi_led_state_cache[led] = led_s;
5693 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005694 default:
5695 return -ENXIO;
5696 }
5697
5698 /* not reached */
5699}
5700
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005701static int led_set_status(const unsigned int led,
5702 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005703{
5704 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005705 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5706 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005707
5708 int rc = 0;
5709
5710 switch (led_supported) {
5711 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005712 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005713 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005714 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005715 if (unlikely(tpacpi_is_led_restricted(led)))
5716 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005717 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5718 (1 << led), led_sled_arg1[ledstatus]))
Pavel Machekaac33112019-04-29 17:21:48 +02005719 return -EIO;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005720 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005721 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005722 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005723 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005724 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005725 if (unlikely(tpacpi_is_led_restricted(led)))
5726 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005727 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5728 if (rc >= 0)
5729 rc = ec_write(TPACPI_LED_EC_HLBL,
5730 (ledstatus == TPACPI_LED_BLINK) << led);
5731 if (rc >= 0)
5732 rc = ec_write(TPACPI_LED_EC_HLCL,
5733 (ledstatus != TPACPI_LED_OFF) << led);
5734 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005735 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005736 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005737 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5738 return -EINVAL;
5739 if (unlikely(tpacpi_is_led_restricted(led)))
5740 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005741 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5742 led, led_led_arg1[ledstatus]))
Pavel Machekaac33112019-04-29 17:21:48 +02005743 return -EIO;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005744 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005745 default:
Pavel Machekaac33112019-04-29 17:21:48 +02005746 return -ENXIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005747 }
5748
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005749 if (!rc)
5750 tpacpi_led_state_cache[led] = ledstatus;
5751
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005752 return rc;
5753}
5754
Hans de Goede86ec0c22017-02-09 16:44:12 +01005755static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005756 enum led_brightness brightness)
5757{
5758 struct tpacpi_led_classdev *data = container_of(led_cdev,
5759 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005760 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005761
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005762 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005763 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005764 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005765 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005766 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005767 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005768
Hans de Goede86ec0c22017-02-09 16:44:12 +01005769 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005770}
5771
5772static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5773 unsigned long *delay_on, unsigned long *delay_off)
5774{
5775 struct tpacpi_led_classdev *data = container_of(led_cdev,
5776 struct tpacpi_led_classdev, led_classdev);
5777
5778 /* Can we choose the flash rate? */
5779 if (*delay_on == 0 && *delay_off == 0) {
5780 /* yes. set them to the hardware blink rate (1 Hz) */
5781 *delay_on = 500; /* ms */
5782 *delay_off = 500; /* ms */
5783 } else if ((*delay_on != 500) || (*delay_off != 500))
5784 return -EINVAL;
5785
Hans de Goede86ec0c22017-02-09 16:44:12 +01005786 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005787}
5788
5789static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5790{
5791 int rc;
5792
5793 struct tpacpi_led_classdev *data = container_of(led_cdev,
5794 struct tpacpi_led_classdev, led_classdev);
5795
5796 rc = led_get_status(data->led);
5797
5798 if (rc == TPACPI_LED_OFF || rc < 0)
5799 rc = LED_OFF; /* no error handling in led class :( */
5800 else
5801 rc = LED_FULL;
5802
5803 return rc;
5804}
5805
5806static void led_exit(void)
5807{
5808 unsigned int i;
5809
Andy Shevchenko41f80042020-05-07 19:37:23 +03005810 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5811 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005812
5813 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005814}
5815
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005816static int __init tpacpi_init_led(unsigned int led)
5817{
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005818 /* LEDs with no name don't get registered */
5819 if (!tpacpi_led_names[led])
5820 return 0;
5821
Hans de Goede86ec0c22017-02-09 16:44:12 +01005822 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005823 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5824 if (led_supported == TPACPI_LED_570)
Andy Shevchenko41f80042020-05-07 19:37:23 +03005825 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005826
5827 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
Hans de Goedee5187042021-11-23 22:05:23 +01005828 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
Andy Shevchenko41f80042020-05-07 19:37:23 +03005829 tpacpi_leds[led].led = led;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005830
Andy Shevchenko41f80042020-05-07 19:37:23 +03005831 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005832}
5833
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005834static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5835 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5836 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5837 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5838
5839 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5840 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5841 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5842 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5843 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5844 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5845 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5846 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5847
5848 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5849 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5850 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5851 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5852 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5853
5854 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5855 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5856 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5857 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5858
5859 /* (1) - may have excess leds enabled on MSB */
5860
5861 /* Defaults (order matters, keep last, don't reorder!) */
5862 { /* Lenovo */
5863 .vendor = PCI_VENDOR_ID_LENOVO,
5864 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5865 .quirks = 0x1fffU,
5866 },
5867 { /* IBM ThinkPads with no EC version string */
5868 .vendor = PCI_VENDOR_ID_IBM,
5869 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5870 .quirks = 0x00ffU,
5871 },
5872 { /* IBM ThinkPads with EC version string */
5873 .vendor = PCI_VENDOR_ID_IBM,
5874 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5875 .quirks = 0x00bfU,
5876 },
5877};
5878
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005879static enum led_access_mode __init led_init_detect_mode(void)
5880{
5881 acpi_status status;
5882
5883 if (tpacpi_is_ibm()) {
5884 /* 570 */
5885 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5886 if (ACPI_SUCCESS(status))
5887 return TPACPI_LED_570;
5888
5889 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5890 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5891 if (ACPI_SUCCESS(status))
5892 return TPACPI_LED_OLD;
5893 }
5894
5895 /* most others */
5896 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5897 if (ACPI_SUCCESS(status))
5898 return TPACPI_LED_NEW;
5899
5900 /* R30, R31, and unknown firmwares */
5901 led_handle = NULL;
5902 return TPACPI_LED_NONE;
5903}
5904
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005905static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005906{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005907 unsigned int i;
5908 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005909 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005910
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005911 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5912
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005913 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005914
Adam Leefcb44e12013-06-07 16:20:08 +08005915 if (led_supported != TPACPI_LED_NONE) {
5916 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5917 ARRAY_SIZE(led_useful_qtable));
5918
5919 if (!useful_leds) {
5920 led_handle = NULL;
5921 led_supported = TPACPI_LED_NONE;
5922 }
5923 }
5924
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005925 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5926 str_supported(led_supported), led_supported);
5927
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005928 if (led_supported == TPACPI_LED_NONE)
Hans de Goedec7e1c782021-11-21 20:11:24 +01005929 return -ENODEV;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005930
Kees Cook6396bb22018-06-12 14:03:40 -07005931 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005932 GFP_KERNEL);
5933 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005934 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005935 return -ENOMEM;
5936 }
5937
5938 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005939 tpacpi_leds[i].led = -1;
5940
Andy Shevchenko41f80042020-05-07 19:37:23 +03005941 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005942 rc = tpacpi_init_led(i);
5943 if (rc < 0) {
5944 led_exit();
5945 return rc;
5946 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005947 }
5948 }
5949
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005950#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03005951 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005952#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005953 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005954}
5955
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005956#define str_led_status(s) \
5957 ((s) == TPACPI_LED_OFF ? "off" : \
5958 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005959
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005960static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005962 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005963 seq_printf(m, "status:\t\tnot supported\n");
5964 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005965 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005966 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005967
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005968 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005969 /* 570 */
5970 int i, status;
5971 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005972 status = led_get_status(i);
5973 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005974 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005975 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005976 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005977 }
5978 }
5979
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03005980 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983}
5984
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005985static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986{
5987 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005988 int led, rc;
5989 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005990
5991 if (!led_supported)
5992 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03005994 while ((cmd = strsep(&buf, ","))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005995 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 return -EINVAL;
5997
Andy Shevchenko41f80042020-05-07 19:37:23 +03005998 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
5999 return -ENODEV;
6000
6001 if (tpacpi_leds[led].led < 0)
Adam Leeedf2d772013-06-08 16:51:15 +08006002 return -ENODEV;
6003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006004 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006005 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006007 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006008 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006009 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006010 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006011 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006012 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006013
6014 rc = led_set_status(led, s);
6015 if (rc < 0)
6016 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 }
6018
6019 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006020}
6021
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006022static struct ibm_struct led_driver_data = {
6023 .name = "led",
6024 .read = led_read,
6025 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006026 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006027};
6028
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006029/*************************************************************************
6030 * Beep subdriver
6031 */
6032
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006033TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006034
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006035#define TPACPI_BEEP_Q1 0x0001
6036
6037static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6038 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6039 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6040};
6041
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006042static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006043{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006044 unsigned long quirks;
6045
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006046 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6047
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006048 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006049
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006050 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6051 str_supported(beep_handle != NULL));
6052
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006053 quirks = tpacpi_check_quirks(beep_quirk_table,
6054 ARRAY_SIZE(beep_quirk_table));
6055
6056 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6057
Hans de Goedec7e1c782021-11-21 20:11:24 +01006058 return (beep_handle) ? 0 : -ENODEV;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006059}
6060
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006061static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006063 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006064 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006065 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006066 seq_printf(m, "status:\t\tsupported\n");
6067 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071}
6072
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006073static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074{
6075 char *cmd;
6076 int beep_cmd;
6077
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006078 if (!beep_handle)
6079 return -ENODEV;
6080
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03006081 while ((cmd = strsep(&buf, ","))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006082 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6083 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 /* beep_cmd set */
6085 } else
6086 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006087 if (tp_features.beep_needs_two_args) {
6088 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6089 beep_cmd, 0))
6090 return -EIO;
6091 } else {
6092 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6093 beep_cmd))
6094 return -EIO;
6095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 }
6097
6098 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006099}
6100
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006101static struct ibm_struct beep_driver_data = {
6102 .name = "beep",
6103 .read = beep_read,
6104 .write = beep_write,
6105};
6106
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006107/*************************************************************************
6108 * Thermal subdriver
6109 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006110
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006111enum thermal_access_mode {
6112 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6113 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6114 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6115 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6116 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6117};
6118
6119enum { /* TPACPI_THERMAL_TPEC_* */
6120 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6121 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
Mark Pearson6759e182021-04-07 17:20:15 -04006122 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006123 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006124
6125 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006126};
6127
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006128
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006129#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6130struct ibm_thermal_sensors_struct {
6131 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6132};
6133
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006134static enum thermal_access_mode thermal_read_mode;
Hans de Goede00cc4262021-04-13 09:21:12 +02006135static bool thermal_use_labels;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006136
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006137/* idx is zero-based */
6138static int thermal_get_sensor(int idx, s32 *value)
6139{
6140 int t;
6141 s8 tmp;
6142 char tmpi[5];
6143
6144 t = TP_EC_THERMAL_TMP0;
6145
6146 switch (thermal_read_mode) {
6147#if TPACPI_MAX_THERMAL_SENSORS >= 16
6148 case TPACPI_THERMAL_TPEC_16:
6149 if (idx >= 8 && idx <= 15) {
6150 t = TP_EC_THERMAL_TMP8;
6151 idx -= 8;
6152 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006153#endif
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006154 fallthrough;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006155 case TPACPI_THERMAL_TPEC_8:
6156 if (idx <= 7) {
6157 if (!acpi_ec_read(t + idx, &tmp))
6158 return -EIO;
6159 *value = tmp * 1000;
6160 return 0;
6161 }
6162 break;
6163
6164 case TPACPI_THERMAL_ACPI_UPDT:
6165 if (idx <= 7) {
6166 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6167 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6168 return -EIO;
6169 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6170 return -EIO;
6171 *value = (t - 2732) * 100;
6172 return 0;
6173 }
6174 break;
6175
6176 case TPACPI_THERMAL_ACPI_TMP07:
6177 if (idx <= 7) {
6178 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6179 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6180 return -EIO;
6181 if (t > 127 || t < -127)
6182 t = TP_EC_THERMAL_TMP_NA;
6183 *value = t * 1000;
6184 return 0;
6185 }
6186 break;
6187
6188 case TPACPI_THERMAL_NONE:
6189 default:
6190 return -ENOSYS;
6191 }
6192
6193 return -EINVAL;
6194}
6195
6196static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6197{
6198 int res, i;
6199 int n;
6200
6201 n = 8;
6202 i = 0;
6203
6204 if (!s)
6205 return -EINVAL;
6206
6207 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6208 n = 16;
6209
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006210 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006211 res = thermal_get_sensor(i, &s->temp[i]);
6212 if (res)
6213 return res;
6214 }
6215
6216 return n;
6217}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006218
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006219static void thermal_dump_all_sensors(void)
6220{
6221 int n, i;
6222 struct ibm_thermal_sensors_struct t;
6223
6224 n = thermal_get_sensors(&t);
6225 if (n <= 0)
6226 return;
6227
Joe Perches0978e012011-04-04 10:06:25 -07006228 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006229
6230 for (i = 0; i < n; i++) {
6231 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006232 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006233 else
Joe Perches0978e012011-04-04 10:06:25 -07006234 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006235 }
6236
Joe Perches0978e012011-04-04 10:06:25 -07006237 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006238}
6239
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006240/* sysfs temp##_input -------------------------------------------------- */
6241
6242static ssize_t thermal_temp_input_show(struct device *dev,
6243 struct device_attribute *attr,
6244 char *buf)
6245{
6246 struct sensor_device_attribute *sensor_attr =
6247 to_sensor_dev_attr(attr);
6248 int idx = sensor_attr->index;
6249 s32 value;
6250 int res;
6251
6252 res = thermal_get_sensor(idx, &value);
6253 if (res)
6254 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006255 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006256 return -ENXIO;
6257
Ye Guojin33ce79b2021-10-18 09:17:50 +00006258 return sysfs_emit(buf, "%d\n", value);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006259}
6260
6261#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006262 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6263 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006264
6265static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6266 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6267 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6268 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6269 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6270 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6271 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6272 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6273 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6274 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6275 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6276 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6277 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6278 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6279 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6280 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6281 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6282};
6283
6284#define THERMAL_ATTRS(X) \
6285 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6286
6287static struct attribute *thermal_temp_input_attr[] = {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006288 THERMAL_ATTRS(0),
6289 THERMAL_ATTRS(1),
6290 THERMAL_ATTRS(2),
6291 THERMAL_ATTRS(3),
6292 THERMAL_ATTRS(4),
6293 THERMAL_ATTRS(5),
6294 THERMAL_ATTRS(6),
6295 THERMAL_ATTRS(7),
Hans de Goede3a0abea2021-11-21 20:11:29 +01006296 THERMAL_ATTRS(8),
6297 THERMAL_ATTRS(9),
6298 THERMAL_ATTRS(10),
6299 THERMAL_ATTRS(11),
6300 THERMAL_ATTRS(12),
6301 THERMAL_ATTRS(13),
6302 THERMAL_ATTRS(14),
6303 THERMAL_ATTRS(15),
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006304 NULL
6305};
6306
Len Baker79f960e2021-10-23 17:40:36 +02006307static umode_t thermal_attr_is_visible(struct kobject *kobj,
6308 struct attribute *attr, int n)
6309{
6310 if (thermal_read_mode == TPACPI_THERMAL_NONE)
6311 return 0;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006312
Len Baker79f960e2021-10-23 17:40:36 +02006313 if (attr == THERMAL_ATTRS(8) || attr == THERMAL_ATTRS(9) ||
6314 attr == THERMAL_ATTRS(10) || attr == THERMAL_ATTRS(11) ||
6315 attr == THERMAL_ATTRS(12) || attr == THERMAL_ATTRS(13) ||
6316 attr == THERMAL_ATTRS(14) || attr == THERMAL_ATTRS(15)) {
6317 if (thermal_read_mode != TPACPI_THERMAL_TPEC_16)
6318 return 0;
6319 }
6320
6321 return attr->mode;
6322}
6323
6324static const struct attribute_group thermal_attr_group = {
6325 .is_visible = thermal_attr_is_visible,
6326 .attrs = thermal_temp_input_attr,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006327};
6328
6329#undef THERMAL_SENSOR_ATTR_TEMP
6330#undef THERMAL_ATTRS
6331
Hans de Goede00cc4262021-04-13 09:21:12 +02006332static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6333{
6334 return sysfs_emit(buf, "CPU\n");
6335}
6336static DEVICE_ATTR_RO(temp1_label);
6337
6338static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6339{
6340 return sysfs_emit(buf, "GPU\n");
6341}
6342static DEVICE_ATTR_RO(temp2_label);
6343
6344static struct attribute *temp_label_attributes[] = {
6345 &dev_attr_temp1_label.attr,
6346 &dev_attr_temp2_label.attr,
6347 NULL
6348};
6349
Len Baker79f960e2021-10-23 17:40:36 +02006350static umode_t temp_label_attr_is_visible(struct kobject *kobj,
6351 struct attribute *attr, int n)
6352{
6353 return thermal_use_labels ? attr->mode : 0;
6354}
6355
Hans de Goede00cc4262021-04-13 09:21:12 +02006356static const struct attribute_group temp_label_attr_group = {
Len Baker79f960e2021-10-23 17:40:36 +02006357 .is_visible = temp_label_attr_is_visible,
Hans de Goede00cc4262021-04-13 09:21:12 +02006358 .attrs = temp_label_attributes,
6359};
6360
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006361/* --------------------------------------------------------------------- */
6362
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006363static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006364{
Mark Pearson6759e182021-04-07 17:20:15 -04006365 u8 t, ta1, ta2, ver = 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006366 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006367 int acpi_tmp7;
6368
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006369 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6370
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006371 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006372
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006373 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006374 /*
6375 * Direct EC access mode: sensors at registers
6376 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6377 * non-implemented, thermal sensors return 0x80 when
6378 * not available
Mark Pearson6759e182021-04-07 17:20:15 -04006379 * The above rule is unfortunately flawed. This has been seen with
6380 * 0xC2 (power supply ID) causing thermal control problems.
6381 * The EC version can be determined by offset 0xEF and at least for
6382 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6383 * are not thermal registers.
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006384 */
Mark Pearson6759e182021-04-07 17:20:15 -04006385 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6386 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006387
6388 ta1 = ta2 = 0;
6389 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006390 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006391 ta1 |= t;
6392 } else {
6393 ta1 = 0;
6394 break;
6395 }
Mark Pearson6759e182021-04-07 17:20:15 -04006396 if (ver < 3) {
6397 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6398 ta2 |= t;
6399 } else {
6400 ta1 = 0;
6401 break;
6402 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006403 }
6404 }
6405 if (ta1 == 0) {
6406 /* This is sheer paranoia, but we handle it anyway */
6407 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006408 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006409 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006410 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006411 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006412 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006413 }
6414 } else {
Hans de Goede00cc4262021-04-13 09:21:12 +02006415 if (ver >= 3) {
Mark Pearson6759e182021-04-07 17:20:15 -04006416 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
Hans de Goede00cc4262021-04-13 09:21:12 +02006417 thermal_use_labels = true;
6418 } else {
Mark Pearson6759e182021-04-07 17:20:15 -04006419 thermal_read_mode =
6420 (ta2 != 0) ?
6421 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Hans de Goede00cc4262021-04-13 09:21:12 +02006422 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006423 }
6424 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006425 if (tpacpi_is_ibm() &&
6426 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006427 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006428 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006429 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006430 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006431 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006432 }
6433 } else {
6434 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006435 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006436 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006437
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006438 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6439 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6440 thermal_read_mode);
6441
Hans de Goedec7e1c782021-11-21 20:11:24 +01006442 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006443}
6444
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006445static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006446{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006447 int n, i;
6448 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006449
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006450 n = thermal_get_sensors(&t);
6451 if (unlikely(n < 0))
6452 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006453
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006454 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006455
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006456 if (n > 0) {
6457 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006458 seq_printf(m, "%d ", t.temp[i] / 1000);
6459 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006460 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006461 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006462
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006463 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006464}
6465
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006466static struct ibm_struct thermal_driver_data = {
6467 .name = "thermal",
6468 .read = thermal_read,
6469};
6470
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006471/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006472 * Backlight/brightness subdriver
6473 */
Holger Macht8acb0252006-10-20 14:30:28 -07006474
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006475#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6476
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006477/*
6478 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6479 * CMOS NVRAM byte 0x5E, bits 0-3.
6480 *
6481 * EC HBRV (0x31) has the following layout
6482 * Bit 7: unknown function
6483 * Bit 6: unknown function
6484 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6485 * Bit 4: must be set to zero to avoid problems
6486 * Bit 3-0: backlight brightness level
6487 *
6488 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006489 *
6490 * WARNING: The X61 has been verified to use HBRV for something else, so
6491 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6492 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006493 */
6494
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006495enum {
6496 TP_EC_BACKLIGHT = 0x31,
6497
6498 /* TP_EC_BACKLIGHT bitmasks */
6499 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6500 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6501 TP_EC_BACKLIGHT_MAPSW = 0x20,
6502};
6503
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006504enum tpacpi_brightness_access_mode {
6505 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6506 TPACPI_BRGHT_MODE_EC, /* EC control */
6507 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6508 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6509 TPACPI_BRGHT_MODE_MAX
6510};
6511
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006512static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006513
6514static enum tpacpi_brightness_access_mode brightness_mode =
6515 TPACPI_BRGHT_MODE_MAX;
6516
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006517static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6518
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006519static struct mutex brightness_mutex;
6520
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006521/* NVRAM brightness access,
6522 * call with brightness_mutex held! */
6523static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006524{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006525 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006526
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006527 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6528 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6529 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006530 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006531
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006532 return lnvram;
6533}
6534
6535static void tpacpi_brightness_checkpoint_nvram(void)
6536{
6537 u8 lec = 0;
6538 u8 b_nvram;
6539
6540 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6541 return;
6542
6543 vdbg_printk(TPACPI_DBG_BRGHT,
6544 "trying to checkpoint backlight level to NVRAM...\n");
6545
6546 if (mutex_lock_killable(&brightness_mutex) < 0)
6547 return;
6548
6549 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6550 goto unlock;
6551 lec &= TP_EC_BACKLIGHT_LVLMSK;
6552 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6553
6554 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6555 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6556 /* NVRAM needs update */
6557 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6558 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6559 b_nvram |= lec;
6560 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6561 dbg_printk(TPACPI_DBG_BRGHT,
6562 "updated NVRAM backlight level to %u (0x%02x)\n",
6563 (unsigned int) lec, (unsigned int) b_nvram);
6564 } else
6565 vdbg_printk(TPACPI_DBG_BRGHT,
6566 "NVRAM backlight level already is %u (0x%02x)\n",
6567 (unsigned int) lec, (unsigned int) b_nvram);
6568
6569unlock:
6570 mutex_unlock(&brightness_mutex);
6571}
6572
6573
6574/* call with brightness_mutex held! */
6575static int tpacpi_brightness_get_raw(int *status)
6576{
6577 u8 lec = 0;
6578
6579 switch (brightness_mode) {
6580 case TPACPI_BRGHT_MODE_UCMS_STEP:
6581 *status = tpacpi_brightness_nvram_get();
6582 return 0;
6583 case TPACPI_BRGHT_MODE_EC:
6584 case TPACPI_BRGHT_MODE_ECNVRAM:
6585 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006586 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006587 *status = lec;
6588 return 0;
6589 default:
6590 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006591 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006592}
6593
6594/* call with brightness_mutex held! */
6595/* do NOT call with illegal backlight level value */
6596static int tpacpi_brightness_set_ec(unsigned int value)
6597{
6598 u8 lec = 0;
6599
6600 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6601 return -EIO;
6602
6603 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6604 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6605 (value & TP_EC_BACKLIGHT_LVLMSK))))
6606 return -EIO;
6607
6608 return 0;
6609}
6610
6611/* call with brightness_mutex held! */
6612static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6613{
6614 int cmos_cmd, inc;
6615 unsigned int current_value, i;
6616
6617 current_value = tpacpi_brightness_nvram_get();
6618
6619 if (value == current_value)
6620 return 0;
6621
6622 cmos_cmd = (value > current_value) ?
6623 TP_CMOS_BRIGHTNESS_UP :
6624 TP_CMOS_BRIGHTNESS_DOWN;
6625 inc = (value > current_value) ? 1 : -1;
6626
6627 for (i = current_value; i != value; i += inc)
6628 if (issue_thinkpad_cmos_command(cmos_cmd))
6629 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006630
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006631 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006632}
6633
6634/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006635static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006636{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006637 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006638
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006639 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006640 return -EINVAL;
6641
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006642 vdbg_printk(TPACPI_DBG_BRGHT,
6643 "set backlight level to %d\n", value);
6644
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006645 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006646 if (res < 0)
6647 return res;
6648
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006649 switch (brightness_mode) {
6650 case TPACPI_BRGHT_MODE_EC:
6651 case TPACPI_BRGHT_MODE_ECNVRAM:
6652 res = tpacpi_brightness_set_ec(value);
6653 break;
6654 case TPACPI_BRGHT_MODE_UCMS_STEP:
6655 res = tpacpi_brightness_set_ucmsstep(value);
6656 break;
6657 default:
6658 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006659 }
6660
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006661 mutex_unlock(&brightness_mutex);
6662 return res;
6663}
6664
6665/* sysfs backlight class ----------------------------------------------- */
6666
6667static int brightness_update_status(struct backlight_device *bd)
6668{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006669 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006670 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6671 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006672 bd->props.brightness : 0;
6673
6674 dbg_printk(TPACPI_DBG_BRGHT,
6675 "backlight: attempt to set level to %d\n",
6676 level);
6677
6678 /* it is the backlight class's job (caller) to handle
6679 * EINTR and other errors properly */
6680 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006681}
Holger Macht8acb0252006-10-20 14:30:28 -07006682
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006683static int brightness_get(struct backlight_device *bd)
6684{
6685 int status, res;
6686
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006687 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006688 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006689 return 0;
6690
6691 res = tpacpi_brightness_get_raw(&status);
6692
6693 mutex_unlock(&brightness_mutex);
6694
6695 if (res < 0)
6696 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006697
6698 return status & TP_EC_BACKLIGHT_LVLMSK;
6699}
6700
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006701static void tpacpi_brightness_notify_change(void)
6702{
6703 backlight_force_update(ibm_backlight_device,
6704 BACKLIGHT_UPDATE_HOTKEY);
6705}
6706
Lionel Debrouxacc24722010-11-16 14:14:02 +01006707static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006708 .get_brightness = brightness_get,
6709 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006710};
6711
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006712/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006713
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006714/*
6715 * Call _BCL method of video device. On some ThinkPads this will
6716 * switch the firmware to the ACPI brightness control mode.
6717 */
6718
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006719static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6720{
6721 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6722 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006723 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006724 int rc;
6725
Aaron Lu46445b62013-10-11 21:27:46 +08006726 if (acpi_bus_get_device(handle, &device))
6727 return 0;
6728
6729 rc = 0;
6730 list_for_each_entry(child, &device->children, node) {
6731 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6732 NULL, &buffer);
Aaron Ma720ef732020-10-03 01:09:16 +08006733 if (ACPI_FAILURE(status)) {
6734 buffer.length = ACPI_ALLOCATE_BUFFER;
Aaron Lu46445b62013-10-11 21:27:46 +08006735 continue;
Aaron Ma720ef732020-10-03 01:09:16 +08006736 }
Aaron Lu46445b62013-10-11 21:27:46 +08006737
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006738 obj = (union acpi_object *)buffer.pointer;
6739 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006740 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006741 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006742 rc = 0;
6743 } else {
6744 rc = obj->package.count;
6745 }
Aaron Lu46445b62013-10-11 21:27:46 +08006746 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006747 }
6748
6749 kfree(buffer.pointer);
6750 return rc;
6751}
6752
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006753
6754/*
6755 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6756 */
6757static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6758{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006759 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006760 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006761
Aaron Lu46445b62013-10-11 21:27:46 +08006762 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006763 if (video_device)
6764 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006765
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006766 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006767
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006768 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006769}
6770
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006771/*
6772 * These are only useful for models that have only one possibility
6773 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6774 * these quirks.
6775 */
6776#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6777#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6778#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6779
6780static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6781 /* Models with ATI GPUs known to require ECNVRAM mode */
6782 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6783
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006784 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006785 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006786 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006787 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006788 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6789
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006790 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006791 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006792 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6793 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006794
6795 /* Models with Intel GMA900 */
6796 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6797 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6798 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6799};
6800
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006801/*
6802 * Returns < 0 for error, otherwise sets tp_features.bright_*
6803 * and bright_maxlvl.
6804 */
6805static void __init tpacpi_detect_brightness_capabilities(void)
6806{
6807 unsigned int b;
6808
6809 vdbg_printk(TPACPI_DBG_INIT,
6810 "detecting firmware brightness interface capabilities\n");
6811
6812 /* we could run a quirks check here (same table used by
6813 * brightness_init) if needed */
6814
6815 /*
6816 * We always attempt to detect acpi support, so as to switch
6817 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6818 * going to publish a backlight interface
6819 */
6820 b = tpacpi_check_std_acpi_brightness_support();
6821 switch (b) {
6822 case 16:
6823 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006824 break;
6825 case 8:
6826 case 0:
6827 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006828 break;
6829 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006830 tp_features.bright_unkfw = 1;
6831 bright_maxlvl = b - 1;
6832 }
Eric Curtin15c75622016-01-30 16:55:59 +00006833 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006834}
6835
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006836static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006837{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006838 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006839 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006840 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006841
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006842 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6843
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006844 mutex_init(&brightness_mutex);
6845
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006846 quirks = tpacpi_check_quirks(brightness_quirk_table,
6847 ARRAY_SIZE(brightness_quirk_table));
6848
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006849 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006850
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006851 /* if it is unknown, we don't handle it: it wouldn't be safe */
6852 if (tp_features.bright_unkfw)
Hans de Goedec7e1c782021-11-21 20:11:24 +01006853 return -ENODEV;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006854
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006855 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006856 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006857 "brightness support disabled by module parameter\n");
Hans de Goedec7e1c782021-11-21 20:11:24 +01006858 return -ENODEV;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006859 }
6860
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006861 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006862 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006863 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Hans de Goedec7e1c782021-11-21 20:11:24 +01006864 return -ENODEV;
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006865 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006866 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
Hans de Goedec7e1c782021-11-21 20:11:24 +01006867 return -ENODEV;
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006868 }
Aaron Ma46713ae2020-07-10 09:56:14 +08006869 } else if (!tp_features.bright_acpimode) {
6870 pr_notice("ACPI backlight interface not available\n");
Hans de Goedec7e1c782021-11-21 20:11:24 +01006871 return -ENODEV;
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006872 }
6873
Aaron Ma46713ae2020-07-10 09:56:14 +08006874 pr_notice("ACPI native brightness control enabled\n");
6875
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006876 /*
6877 * Check for module parameter bogosity, note that we
6878 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6879 * able to detect "unspecified"
6880 */
6881 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006882 return -EINVAL;
6883
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006884 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6885 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6886 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006887 if (quirks & TPACPI_BRGHT_Q_EC)
6888 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6889 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006890 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6891
6892 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006893 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006894 brightness_mode);
6895 }
6896
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006897 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006898 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006899 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6900 brightness_mode == TPACPI_BRGHT_MODE_EC))
6901 return -EINVAL;
6902
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006903 if (tpacpi_brightness_get_raw(&b) < 0)
Hans de Goedec7e1c782021-11-21 20:11:24 +01006904 return -ENODEV;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006905
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006906 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006907 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006908 props.max_brightness = bright_maxlvl;
6909 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006910 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6911 NULL, NULL,
6912 &ibm_backlight_data,
6913 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006914 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006915 int rc = PTR_ERR(ibm_backlight_device);
6916 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006917 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006918 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006919 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006920 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6921 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006922
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006923 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006924 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6925 brightness_mode);
6926 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6927 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006928 }
6929
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006930 /* Added by mistake in early 2007. Probably useless, but it could
6931 * be working around some unknown firmware problem where the value
6932 * read at startup doesn't match the real hardware state... so leave
6933 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006934 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006935
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006936 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006937 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006938 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6939 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006940 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006941 return 0;
6942}
6943
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006944static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006945{
6946 tpacpi_brightness_checkpoint_nvram();
6947}
6948
6949static void brightness_shutdown(void)
6950{
6951 tpacpi_brightness_checkpoint_nvram();
6952}
6953
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006954static void brightness_exit(void)
6955{
6956 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006957 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006958 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006959 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006960 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006961
6962 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006963}
6964
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006965static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006966{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006967 int level;
6968
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006969 level = brightness_get(NULL);
6970 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006971 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006972 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006973 seq_printf(m, "level:\t\t%d\n", level);
6974 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006975 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6976 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006977 }
6978
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006979 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006980}
6981
6982static int brightness_write(char *buf)
6983{
6984 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006985 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006986 char *cmd;
6987
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006988 level = brightness_get(NULL);
6989 if (level < 0)
6990 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006991
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03006992 while ((cmd = strsep(&buf, ","))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006993 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006994 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006995 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006996 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006997 if (level > 0)
6998 level--;
6999 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007000 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007001 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007002 } else
7003 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007004 }
7005
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007006 tpacpi_disclose_usertask("procfs brightness",
7007 "set level to %d\n", level);
7008
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007009 /*
7010 * Now we know what the final level should be, so we try to set it.
7011 * Doing it this way makes the syscall restartable in case of EINTR
7012 */
7013 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007014 if (!rc && ibm_backlight_device)
7015 backlight_force_update(ibm_backlight_device,
7016 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007017 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007018}
7019
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007020static struct ibm_struct brightness_driver_data = {
7021 .name = "brightness",
7022 .read = brightness_read,
7023 .write = brightness_write,
7024 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007025 .suspend = brightness_suspend,
7026 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007027};
7028
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007029/*************************************************************************
7030 * Volume subdriver
7031 */
7032
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007033/*
7034 * IBM ThinkPads have a simple volume controller with MUTE gating.
7035 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7036 *
7037 * Since the *61 series (and probably also the later *60 series), Lenovo
7038 * ThinkPads only implement the MUTE gate.
7039 *
7040 * EC register 0x30
7041 * Bit 6: MUTE (1 mutes sound)
7042 * Bit 3-0: Volume
7043 * Other bits should be zero as far as we know.
7044 *
7045 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7046 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7047 * such as bit 7 which is used to detect repeated presses of MUTE,
7048 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007049 *
7050 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7051 * in response to user input. Unfortunately, this rarely works well.
7052 * The laptop changes the state of its internal MUTE gate and, on some
7053 * models, sends KEY_MUTE, causing any user code that responds to the
7054 * mute button to get confused. The hardware MUTE gate is also
7055 * unnecessary, since user code can handle the mute button without
7056 * kernel or EC help.
7057 *
7058 * To avoid confusing userspace, we simply disable all EC-based mute
7059 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007060 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007061
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007062#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7063
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007064#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7065#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7066#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7067
Takashi Iwaicab66612013-10-24 16:06:32 +02007068#if SNDRV_CARDS <= 32
7069#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7070#else
7071#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7072#endif
7073static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007074static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307075static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007076
7077struct tpacpi_alsa_data {
7078 struct snd_card *card;
7079 struct snd_ctl_elem_id *ctl_mute_id;
7080 struct snd_ctl_elem_id *ctl_vol_id;
7081};
7082
7083static struct snd_card *alsa_card;
7084
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007085enum {
7086 TP_EC_AUDIO = 0x30,
7087
7088 /* TP_EC_AUDIO bits */
7089 TP_EC_AUDIO_MUTESW = 6,
7090
7091 /* TP_EC_AUDIO bitmasks */
7092 TP_EC_AUDIO_LVL_MSK = 0x0F,
7093 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7094
7095 /* Maximum volume */
7096 TP_EC_VOLUME_MAX = 14,
7097};
7098
7099enum tpacpi_volume_access_mode {
7100 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7101 TPACPI_VOL_MODE_EC, /* Pure EC control */
7102 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7103 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7104 TPACPI_VOL_MODE_MAX
7105};
7106
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007107enum tpacpi_volume_capabilities {
7108 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7109 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7110 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7111 TPACPI_VOL_CAP_MAX
7112};
7113
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007114enum tpacpi_mute_btn_mode {
7115 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7116 /* We don't know what mode 1 is. */
7117 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7118 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7119};
7120
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007121static enum tpacpi_volume_access_mode volume_mode =
7122 TPACPI_VOL_MODE_MAX;
7123
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007124static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307125static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007126static bool software_mute_requested = true;
7127static bool software_mute_active;
7128static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007129
7130/*
7131 * Used to syncronize writers to TP_EC_AUDIO and
7132 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7133 */
7134static struct mutex volume_mutex;
7135
7136static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007137{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007138 u8 lec = 0;
7139 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007140 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007141
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007142 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7143 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007144 if (!volume_control_allowed)
7145 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007146 if (software_mute_active)
7147 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007148
7149 vdbg_printk(TPACPI_DBG_MIXER,
7150 "trying to checkpoint mixer state to NVRAM...\n");
7151
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007152 if (tp_features.mixer_no_level_control)
7153 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7154 else
7155 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7156
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007157 if (mutex_lock_killable(&volume_mutex) < 0)
7158 return;
7159
7160 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7161 goto unlock;
7162 lec &= ec_mask;
7163 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7164
7165 if (lec != (b_nvram & ec_mask)) {
7166 /* NVRAM needs update */
7167 b_nvram &= ~ec_mask;
7168 b_nvram |= lec;
7169 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7170 dbg_printk(TPACPI_DBG_MIXER,
7171 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7172 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007173 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007174 vdbg_printk(TPACPI_DBG_MIXER,
7175 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7176 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007177 }
7178
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007179unlock:
7180 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007181}
7182
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007183static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007184{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007185 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007186
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007187 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7188 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007189
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007190 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007191
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007192 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007193
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007194 return 0;
7195}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007196
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007197static int volume_get_status(u8 *status)
7198{
7199 return volume_get_status_ec(status);
7200}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007201
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007202static int volume_set_status_ec(const u8 status)
7203{
7204 if (!acpi_ec_write(TP_EC_AUDIO, status))
7205 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007206
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007207 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7208
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007209 /*
7210 * On X200s, and possibly on others, it can take a while for
7211 * reads to become correct.
7212 */
7213 msleep(1);
7214
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007215 return 0;
7216}
7217
7218static int volume_set_status(const u8 status)
7219{
7220 return volume_set_status_ec(status);
7221}
7222
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007223/* returns < 0 on error, 0 on no change, 1 on change */
7224static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007225{
7226 int rc;
7227 u8 s, n;
7228
7229 if (mutex_lock_killable(&volume_mutex) < 0)
7230 return -EINTR;
7231
7232 rc = volume_get_status_ec(&s);
7233 if (rc)
7234 goto unlock;
7235
7236 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7237 s & ~TP_EC_AUDIO_MUTESW_MSK;
7238
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007239 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007240 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007241 if (!rc)
7242 rc = 1;
7243 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007244
7245unlock:
7246 mutex_unlock(&volume_mutex);
7247 return rc;
7248}
7249
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007250static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007251{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007252 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007253 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007254 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007255}
7256
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007257static int volume_set_mute(const bool mute)
7258{
7259 int rc;
7260
7261 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7262 (mute) ? "" : "un");
7263
7264 rc = __volume_set_mute_ec(mute);
7265 return (rc < 0) ? rc : 0;
7266}
7267
7268/* returns < 0 on error, 0 on no change, 1 on change */
7269static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007270{
7271 int rc;
7272 u8 s, n;
7273
7274 if (vol > TP_EC_VOLUME_MAX)
7275 return -EINVAL;
7276
7277 if (mutex_lock_killable(&volume_mutex) < 0)
7278 return -EINTR;
7279
7280 rc = volume_get_status_ec(&s);
7281 if (rc)
7282 goto unlock;
7283
7284 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7285
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007286 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007287 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007288 if (!rc)
7289 rc = 1;
7290 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007291
7292unlock:
7293 mutex_unlock(&volume_mutex);
7294 return rc;
7295}
7296
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007297static int volume_set_software_mute(bool startup)
7298{
7299 int result;
7300
7301 if (!tpacpi_is_lenovo())
7302 return -ENODEV;
7303
7304 if (startup) {
7305 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7306 "HAUM", "qd"))
7307 return -EIO;
7308
7309 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7310 "Initial HAUM setting was %d\n",
7311 software_mute_orig_mode);
7312 }
7313
7314 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7315 (int)TP_EC_MUTE_BTN_NONE))
7316 return -EIO;
7317
7318 if (result != TP_EC_MUTE_BTN_NONE)
7319 pr_warn("Unexpected SAUM result %d\n",
7320 result);
7321
7322 /*
7323 * In software mute mode, the standard codec controls take
7324 * precendence, so we unmute the ThinkPad HW switch at
7325 * startup. Just on case there are SAUM-capable ThinkPads
7326 * with level controls, set max HW volume as well.
7327 */
7328 if (tp_features.mixer_no_level_control)
7329 result = volume_set_mute(false);
7330 else
7331 result = volume_set_status(TP_EC_VOLUME_MAX);
7332
7333 if (result != 0)
7334 pr_warn("Failed to unmute the HW mute switch\n");
7335
7336 return 0;
7337}
7338
7339static void volume_exit_software_mute(void)
7340{
7341 int r;
7342
7343 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7344 || r != software_mute_orig_mode)
7345 pr_warn("Failed to restore mute mode\n");
7346}
7347
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007348static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007349{
7350 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007351 "ALSA: trying to set volume level to %hu\n", vol);
7352 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007353}
7354
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007355static void volume_alsa_notify_change(void)
7356{
7357 struct tpacpi_alsa_data *d;
7358
7359 if (alsa_card && alsa_card->private_data) {
7360 d = alsa_card->private_data;
7361 if (d->ctl_mute_id)
7362 snd_ctl_notify(alsa_card,
7363 SNDRV_CTL_EVENT_MASK_VALUE,
7364 d->ctl_mute_id);
7365 if (d->ctl_vol_id)
7366 snd_ctl_notify(alsa_card,
7367 SNDRV_CTL_EVENT_MASK_VALUE,
7368 d->ctl_vol_id);
7369 }
7370}
7371
7372static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7373 struct snd_ctl_elem_info *uinfo)
7374{
7375 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7376 uinfo->count = 1;
7377 uinfo->value.integer.min = 0;
7378 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7379 return 0;
7380}
7381
7382static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7383 struct snd_ctl_elem_value *ucontrol)
7384{
7385 u8 s;
7386 int rc;
7387
7388 rc = volume_get_status(&s);
7389 if (rc < 0)
7390 return rc;
7391
7392 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7393 return 0;
7394}
7395
7396static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7397 struct snd_ctl_elem_value *ucontrol)
7398{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007399 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7400 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007401 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007402}
7403
7404#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7405
7406static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7407 struct snd_ctl_elem_value *ucontrol)
7408{
7409 u8 s;
7410 int rc;
7411
7412 rc = volume_get_status(&s);
7413 if (rc < 0)
7414 return rc;
7415
7416 ucontrol->value.integer.value[0] =
7417 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7418 return 0;
7419}
7420
7421static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7422 struct snd_ctl_elem_value *ucontrol)
7423{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007424 tpacpi_disclose_usertask("ALSA", "%smute\n",
7425 ucontrol->value.integer.value[0] ?
7426 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007427 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007428}
7429
Mathias Krausec3aa4722014-07-16 19:43:17 +02007430static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007431 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7432 .name = "Console Playback Volume",
7433 .index = 0,
7434 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7435 .info = volume_alsa_vol_info,
7436 .get = volume_alsa_vol_get,
7437};
7438
Mathias Krausec3aa4722014-07-16 19:43:17 +02007439static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7441 .name = "Console Playback Switch",
7442 .index = 0,
7443 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7444 .info = volume_alsa_mute_info,
7445 .get = volume_alsa_mute_get,
7446};
7447
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007448static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007449{
7450 tpacpi_volume_checkpoint_nvram();
7451}
7452
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007453static void volume_resume(void)
7454{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007455 if (software_mute_active) {
7456 if (volume_set_software_mute(false) < 0)
7457 pr_warn("Failed to restore software mute\n");
7458 } else {
7459 volume_alsa_notify_change();
7460 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007461}
7462
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007463static void volume_shutdown(void)
7464{
7465 tpacpi_volume_checkpoint_nvram();
7466}
7467
7468static void volume_exit(void)
7469{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007470 if (alsa_card) {
7471 snd_card_free(alsa_card);
7472 alsa_card = NULL;
7473 }
7474
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007475 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007476
7477 if (software_mute_active)
7478 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007479}
7480
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007481static int __init volume_create_alsa_mixer(void)
7482{
7483 struct snd_card *card;
7484 struct tpacpi_alsa_data *data;
7485 struct snd_kcontrol *ctl_vol;
7486 struct snd_kcontrol *ctl_mute;
7487 int rc;
7488
Takashi Iwai89235e52014-01-29 15:01:27 +01007489 rc = snd_card_new(&tpacpi_pdev->dev,
7490 alsa_index, alsa_id, THIS_MODULE,
7491 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007492 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007493 pr_err("Failed to create ALSA card structures: %d\n", rc);
Hans de Goedec7e1c782021-11-21 20:11:24 +01007494 return -ENODEV;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007495 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007496
7497 BUG_ON(!card->private_data);
7498 data = card->private_data;
7499 data->card = card;
7500
7501 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7502 sizeof(card->driver));
7503 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7504 sizeof(card->shortname));
7505 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7506 (thinkpad_id.ec_version_str) ?
7507 thinkpad_id.ec_version_str : "(unknown)");
7508 snprintf(card->longname, sizeof(card->longname),
7509 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7510 (thinkpad_id.ec_version_str) ?
7511 thinkpad_id.ec_version_str : "unknown");
7512
7513 if (volume_control_allowed) {
7514 volume_alsa_control_vol.put = volume_alsa_vol_put;
7515 volume_alsa_control_vol.access =
7516 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7517
7518 volume_alsa_control_mute.put = volume_alsa_mute_put;
7519 volume_alsa_control_mute.access =
7520 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7521 }
7522
7523 if (!tp_features.mixer_no_level_control) {
7524 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7525 rc = snd_ctl_add(card, ctl_vol);
7526 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007527 pr_err("Failed to create ALSA volume control: %d\n",
7528 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007529 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007530 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007531 data->ctl_vol_id = &ctl_vol->id;
7532 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007533
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007534 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7535 rc = snd_ctl_add(card, ctl_mute);
7536 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007537 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007538 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007539 }
7540 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007541
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007542 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007543 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007544 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007545 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007546 }
7547
7548 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007549 return 0;
7550
7551err_exit:
7552 snd_card_free(card);
Hans de Goedec7e1c782021-11-21 20:11:24 +01007553 return -ENODEV;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007554}
7555
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007556#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7557#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7558
7559static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7560 /* Whitelist volume level on all IBM by default */
7561 { .vendor = PCI_VENDOR_ID_IBM,
7562 .bios = TPACPI_MATCH_ANY,
7563 .ec = TPACPI_MATCH_ANY,
7564 .quirks = TPACPI_VOL_Q_LEVEL },
7565
7566 /* Lenovo models with volume control (needs confirmation) */
7567 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7568 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7569 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7570 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7571 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7572 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7573 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7574
7575 /* Whitelist mute-only on all Lenovo by default */
7576 { .vendor = PCI_VENDOR_ID_LENOVO,
7577 .bios = TPACPI_MATCH_ANY,
7578 .ec = TPACPI_MATCH_ANY,
7579 .quirks = TPACPI_VOL_Q_MUTEONLY }
7580};
7581
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007582static int __init volume_init(struct ibm_init_struct *iibm)
7583{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007584 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007585 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007586
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007587 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7588
7589 mutex_init(&volume_mutex);
7590
7591 /*
7592 * Check for module parameter bogosity, note that we
7593 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7594 * able to detect "unspecified"
7595 */
7596 if (volume_mode > TPACPI_VOL_MODE_MAX)
7597 return -EINVAL;
7598
7599 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007600 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7601 TPACPI_MAIL);
Hans de Goedec7e1c782021-11-21 20:11:24 +01007602 return -ENODEV;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007603 }
7604
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007605 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7606 return -EINVAL;
7607
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007608 /*
7609 * The ALSA mixer is our primary interface.
7610 * When disabled, don't install the subdriver at all
7611 */
7612 if (!alsa_enable) {
7613 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007614 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Hans de Goedec7e1c782021-11-21 20:11:24 +01007615 return -ENODEV;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007616 }
7617
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007618 quirks = tpacpi_check_quirks(volume_quirk_table,
7619 ARRAY_SIZE(volume_quirk_table));
7620
7621 switch (volume_capabilities) {
7622 case TPACPI_VOL_CAP_AUTO:
7623 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7624 tp_features.mixer_no_level_control = 1;
7625 else if (quirks & TPACPI_VOL_Q_LEVEL)
7626 tp_features.mixer_no_level_control = 0;
7627 else
Hans de Goedec7e1c782021-11-21 20:11:24 +01007628 return -ENODEV; /* no mixer */
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007629 break;
7630 case TPACPI_VOL_CAP_VOLMUTE:
7631 tp_features.mixer_no_level_control = 0;
7632 break;
7633 case TPACPI_VOL_CAP_MUTEONLY:
7634 tp_features.mixer_no_level_control = 1;
7635 break;
7636 default:
Hans de Goedec7e1c782021-11-21 20:11:24 +01007637 return -ENODEV;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007638 }
7639
7640 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7641 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7642 "using user-supplied volume_capabilities=%d\n",
7643 volume_capabilities);
7644
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007645 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7646 volume_mode == TPACPI_VOL_MODE_MAX) {
7647 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7648
7649 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7650 "driver auto-selected volume_mode=%d\n",
7651 volume_mode);
7652 } else {
7653 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7654 "using user-supplied volume_mode=%d\n",
7655 volume_mode);
7656 }
7657
7658 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007659 "mute is supported, volume control is %s\n",
7660 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007661
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007662 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7663 software_mute_active = true;
7664 } else {
7665 rc = volume_create_alsa_mixer();
7666 if (rc) {
7667 pr_err("Could not create the ALSA mixer interface\n");
7668 return rc;
7669 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007670
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007671 pr_info("Console audio control enabled, mode: %s\n",
7672 (volume_control_allowed) ?
7673 "override (read/write)" :
7674 "monitor (read only)");
7675 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007676
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007677 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7678 "registering volume hotkeys as change notification\n");
7679 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7680 | TP_ACPI_HKEY_VOLUP_MASK
7681 | TP_ACPI_HKEY_VOLDWN_MASK
7682 | TP_ACPI_HKEY_MUTE_MASK);
7683
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007684 return 0;
7685}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007686
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007687static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007688{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007689 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007690
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007691 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007692 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007693 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007694 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007695 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007696 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007697 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007698 status & TP_EC_AUDIO_LVL_MSK);
7699
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007700 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007701 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007702
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007703 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007704 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007705 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007706 seq_printf(m, "commands:\tup, down\n");
7707 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7708 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007709 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007710 }
7711 }
7712
7713 return 0;
7714}
7715
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007716static int volume_write(char *buf)
7717{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007718 u8 s;
7719 u8 new_level, new_mute;
7720 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007721 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007722 int rc;
7723
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007724 /*
7725 * We do allow volume control at driver startup, so that the
7726 * user can set initial state through the volume=... parameter hack.
7727 */
7728 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7729 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7730 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007731 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7732 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007733 }
7734 return -EPERM;
7735 }
7736
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007737 rc = volume_get_status(&s);
7738 if (rc < 0)
7739 return rc;
7740
7741 new_level = s & TP_EC_AUDIO_LVL_MSK;
7742 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007743
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03007744 while ((cmd = strsep(&buf, ","))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007745 if (!tp_features.mixer_no_level_control) {
7746 if (strlencmp(cmd, "up") == 0) {
7747 if (new_mute)
7748 new_mute = 0;
7749 else if (new_level < TP_EC_VOLUME_MAX)
7750 new_level++;
7751 continue;
7752 } else if (strlencmp(cmd, "down") == 0) {
7753 if (new_mute)
7754 new_mute = 0;
7755 else if (new_level > 0)
7756 new_level--;
7757 continue;
7758 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7759 l >= 0 && l <= TP_EC_VOLUME_MAX) {
Jiapeng Chong3fca4b12021-05-24 14:23:58 +08007760 new_level = l;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007761 continue;
7762 }
7763 }
7764 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007765 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007766 else if (strlencmp(cmd, "unmute") == 0)
7767 new_mute = 0;
7768 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007769 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007770 }
7771
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007772 if (tp_features.mixer_no_level_control) {
7773 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7774 new_mute ? "" : "un");
7775 rc = volume_set_mute(!!new_mute);
7776 } else {
7777 tpacpi_disclose_usertask("procfs volume",
7778 "%smute and set level to %d\n",
7779 new_mute ? "" : "un", new_level);
7780 rc = volume_set_status(new_mute | new_level);
7781 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007782 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007783
7784 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007785}
7786
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007787static struct ibm_struct volume_driver_data = {
7788 .name = "volume",
7789 .read = volume_read,
7790 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007791 .exit = volume_exit,
7792 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007793 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007794 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007795};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007796
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007797#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7798
7799#define alsa_card NULL
7800
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007801static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007802{
7803}
7804
7805static int __init volume_init(struct ibm_init_struct *iibm)
7806{
Joe Perches0978e012011-04-04 10:06:25 -07007807 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007808
Hans de Goedec7e1c782021-11-21 20:11:24 +01007809 return -ENODEV;
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007810}
7811
7812static struct ibm_struct volume_driver_data = {
7813 .name = "volume",
7814};
7815
7816#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7817
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007818/*************************************************************************
7819 * Fan subdriver
7820 */
7821
7822/*
7823 * FAN ACCESS MODES
7824 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007825 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007826 * ACPI GFAN method: returns fan level
7827 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007828 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007829 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007830 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007831 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007832 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7833 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007834 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7835 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007836 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007837 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007838 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7839 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007840 *
7841 * Fan speed changes of any sort (including those caused by the
7842 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007843 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007844 * limited.
7845 *
7846 * Reading is not available if GFAN exists.
7847 * Writing is not available if SFAN exists.
7848 *
7849 * Bits
7850 * 7 automatic mode engaged;
7851 * (default operation mode of the ThinkPad)
7852 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007853 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007854 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007855 * the tachometer while the fan controller ramps up
7856 * the speed (which can take up to a few *minutes*).
7857 * Speeds up fan to 100% duty-cycle, which is far above
7858 * the standard RPM levels. It is not impossible that
7859 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007860 * 5-3 unused in some models. Extra bits for fan level
7861 * in others, but still useless as all values above
7862 * 7 map to the same speed as level 7 in these models.
7863 * 2-0 fan level (0..7 usually)
7864 * 0x00 = stop
7865 * 0x07 = max (set when temperatures critical)
7866 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007867 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007868 *
7869 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007870 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007871 * does so, its initial value is meaningless (0x07).
7872 *
7873 * For firmware bugs, refer to:
Alexander A. Klimov19206a12020-07-13 21:06:28 +02007874 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007875 *
7876 * ----
7877 *
7878 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7879 * Main fan tachometer reading (in RPM)
7880 *
7881 * This register is present on all ThinkPads with a new-style EC, and
7882 * it is known not to be present on the A21m/e, and T22, as there is
7883 * something else in offset 0x84 according to the ACPI DSDT. Other
7884 * ThinkPads from this same time period (and earlier) probably lack the
7885 * tachometer as well.
7886 *
Nick Andrew877d0312009-01-26 11:06:57 +01007887 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007888 * was never fixed by IBM to report the EC firmware version string
7889 * probably support the tachometer (like the early X models), so
7890 * detecting it is quite hard. We need more data to know for sure.
7891 *
7892 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7893 * might result.
7894 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007895 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7896 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007897 *
7898 * For firmware bugs, refer to:
Alexander A. Klimov19206a12020-07-13 21:06:28 +02007899 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007900 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007901 * ----
7902 *
7903 * ThinkPad EC register 0x31 bit 0 (only on select models)
7904 *
7905 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7906 * show the speed of the main fan. When bit 0 of EC register 0x31
7907 * is one, the tachometer registers show the speed of the auxiliary
7908 * fan.
7909 *
7910 * Fan control seems to affect both fans, regardless of the state
7911 * of this bit.
7912 *
7913 * So far, only the firmware for the X60/X61 non-tablet versions
7914 * seem to support this (firmware TP-7M).
7915 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007916 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007917 * ThinkPad X31, X40, X41. Not available in the X60.
7918 *
7919 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7920 * high speed. ACPI DSDT seems to map these three speeds to levels
7921 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7922 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7923 *
7924 * The speeds are stored on handles
7925 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7926 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007927 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007928 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7929 *
7930 * ACPI DSDT switches which set is in use depending on various
7931 * factors.
7932 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007933 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007934 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7935 * but the ACPI tables just mention level 7.
7936 */
7937
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007938enum { /* Fan control constants */
7939 fan_status_offset = 0x2f, /* EC register 0x2f */
7940 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7941 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007942 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7943 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007944
7945 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7946 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7947
7948 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7949};
7950
7951enum fan_status_access_mode {
7952 TPACPI_FAN_NONE = 0, /* No fan status or control */
7953 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7954 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7955};
7956
7957enum fan_control_access_mode {
7958 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7959 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7960 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7961 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7962};
7963
7964enum fan_control_commands {
7965 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7966 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7967 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7968 * and also watchdog cmd */
7969};
7970
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307971static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007972
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007973static enum fan_status_access_mode fan_status_access_mode;
7974static enum fan_control_access_mode fan_control_access_mode;
7975static enum fan_control_commands fan_control_commands;
7976
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007977static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007978static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007979static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007980static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007981
7982static struct mutex fan_mutex;
7983
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007984static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007985static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007986
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007987TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7988TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007989 "\\FSPD", /* 600e/x, 770e, 770x */
7990 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007991TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007992 "JFNS", /* 770x-JL */
7993 ); /* all others */
7994
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007995/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007996 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7997 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7998 * be in auto mode (0x80).
7999 *
8000 * This is corrected by any write to HFSP either by the driver, or
8001 * by the firmware.
8002 *
8003 * We assume 0x07 really means auto mode while this quirk is active,
8004 * as this is far more likely than the ThinkPad being in level 7,
8005 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008006 *
8007 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8008 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008009 */
8010
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008011static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008012{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008013 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008014 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 -03008015 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008016 }
8017}
8018
8019static void fan_quirk1_handle(u8 *fan_status)
8020{
8021 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8022 if (*fan_status != fan_control_initial_status) {
8023 /* something changed the HFSP regisnter since
8024 * driver init time, so it is not undefined
8025 * anymore */
8026 tp_features.fan_ctrl_status_undef = 0;
8027 } else {
8028 /* Return most likely status. In fact, it
8029 * might be the only possible status */
8030 *fan_status = TP_EC_FAN_AUTO;
8031 }
8032 }
8033}
8034
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008035/* Select main fan on X60/X61, NOOP on others */
8036static bool fan_select_fan1(void)
8037{
8038 if (tp_features.second_fan) {
8039 u8 val;
8040
8041 if (ec_read(fan_select_offset, &val) < 0)
8042 return false;
8043 val &= 0xFEU;
8044 if (ec_write(fan_select_offset, val) < 0)
8045 return false;
8046 }
8047 return true;
8048}
8049
8050/* Select secondary fan on X60/X61 */
8051static bool fan_select_fan2(void)
8052{
8053 u8 val;
8054
8055 if (!tp_features.second_fan)
8056 return false;
8057
8058 if (ec_read(fan_select_offset, &val) < 0)
8059 return false;
8060 val |= 0x01U;
8061 if (ec_write(fan_select_offset, val) < 0)
8062 return false;
8063
8064 return true;
8065}
8066
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008067/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008068 * Call with fan_mutex held
8069 */
8070static void fan_update_desired_level(u8 status)
8071{
8072 if ((status &
8073 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8074 if (status > 7)
8075 fan_control_desired_level = 7;
8076 else
8077 fan_control_desired_level = status;
8078 }
8079}
8080
8081static int fan_get_status(u8 *status)
8082{
8083 u8 s;
8084
8085 /* TODO:
8086 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8087
8088 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008089 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008090 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008091 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008092
Dan Carpentereceeb432012-09-01 12:54:07 -07008093 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008094 return -EIO;
8095
8096 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008097 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008098
8099 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008100 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008101 case TPACPI_FAN_RD_TPEC:
8102 /* all except 570, 600e/x, 770e, 770x */
8103 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8104 return -EIO;
8105
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008106 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008107 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008108 fan_quirk1_handle(status);
8109 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008110
8111 break;
8112
8113 default:
8114 return -ENXIO;
8115 }
8116
8117 return 0;
8118}
8119
8120static int fan_get_status_safe(u8 *status)
8121{
8122 int rc;
8123 u8 s;
8124
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008125 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008126 return -ERESTARTSYS;
8127 rc = fan_get_status(&s);
8128 if (!rc)
8129 fan_update_desired_level(s);
8130 mutex_unlock(&fan_mutex);
8131
Dan Carpentera7718362016-04-15 17:46:34 +03008132 if (rc)
8133 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008134 if (status)
8135 *status = s;
8136
Dan Carpentera7718362016-04-15 17:46:34 +03008137 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008138}
8139
8140static int fan_get_speed(unsigned int *speed)
8141{
8142 u8 hi, lo;
8143
8144 switch (fan_status_access_mode) {
8145 case TPACPI_FAN_RD_TPEC:
8146 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008147 if (unlikely(!fan_select_fan1()))
8148 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008149 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8150 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8151 return -EIO;
8152
8153 if (likely(speed))
8154 *speed = (hi << 8) | lo;
8155
8156 break;
8157
8158 default:
8159 return -ENXIO;
8160 }
8161
8162 return 0;
8163}
8164
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008165static int fan2_get_speed(unsigned int *speed)
8166{
8167 u8 hi, lo;
8168 bool rc;
8169
8170 switch (fan_status_access_mode) {
8171 case TPACPI_FAN_RD_TPEC:
8172 /* all except 570, 600e/x, 770e, 770x */
8173 if (unlikely(!fan_select_fan2()))
8174 return -EIO;
8175 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8176 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8177 fan_select_fan1(); /* play it safe */
8178 if (rc)
8179 return -EIO;
8180
8181 if (likely(speed))
8182 *speed = (hi << 8) | lo;
8183
8184 break;
8185
8186 default:
8187 return -ENXIO;
8188 }
8189
8190 return 0;
8191}
8192
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008193static int fan_set_level(int level)
8194{
8195 if (!fan_control_allowed)
8196 return -EPERM;
8197
8198 switch (fan_control_access_mode) {
8199 case TPACPI_FAN_WR_ACPI_SFAN:
Lars Hofhansl14232c62020-04-23 14:57:09 -07008200 if ((level < 0) || (level > 7))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008201 return -EINVAL;
Lars Hofhansl14232c62020-04-23 14:57:09 -07008202
8203 if (tp_features.second_fan_ctl) {
8204 if (!fan_select_fan2() ||
8205 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8206 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8207 tp_features.second_fan_ctl = 0;
8208 }
8209 fan_select_fan1();
8210 }
8211 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8212 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008213 break;
8214
8215 case TPACPI_FAN_WR_ACPI_FANS:
8216 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008217 if (!(level & TP_EC_FAN_AUTO) &&
8218 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008219 ((level < 0) || (level > 7)))
8220 return -EINVAL;
8221
8222 /* safety net should the EC not support AUTO
8223 * or FULLSPEED mode bits and just ignore them */
8224 if (level & TP_EC_FAN_FULLSPEED)
8225 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008226 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008227 level |= 4; /* safety min speed 4 */
8228
Lars Hofhansl14232c62020-04-23 14:57:09 -07008229 if (tp_features.second_fan_ctl) {
8230 if (!fan_select_fan2() ||
8231 !acpi_ec_write(fan_status_offset, level)) {
8232 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8233 tp_features.second_fan_ctl = 0;
8234 }
8235 fan_select_fan1();
8236
8237 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008238 if (!acpi_ec_write(fan_status_offset, level))
8239 return -EIO;
8240 else
8241 tp_features.fan_ctrl_status_undef = 0;
8242 break;
8243
8244 default:
8245 return -ENXIO;
8246 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008247
8248 vdbg_printk(TPACPI_DBG_FAN,
8249 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008250 return 0;
8251}
8252
8253static int fan_set_level_safe(int level)
8254{
8255 int rc;
8256
8257 if (!fan_control_allowed)
8258 return -EPERM;
8259
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008260 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008261 return -ERESTARTSYS;
8262
8263 if (level == TPACPI_FAN_LAST_LEVEL)
8264 level = fan_control_desired_level;
8265
8266 rc = fan_set_level(level);
8267 if (!rc)
8268 fan_update_desired_level(level);
8269
8270 mutex_unlock(&fan_mutex);
8271 return rc;
8272}
8273
8274static int fan_set_enable(void)
8275{
8276 u8 s;
8277 int rc;
8278
8279 if (!fan_control_allowed)
8280 return -EPERM;
8281
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008282 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008283 return -ERESTARTSYS;
8284
8285 switch (fan_control_access_mode) {
8286 case TPACPI_FAN_WR_ACPI_FANS:
8287 case TPACPI_FAN_WR_TPEC:
8288 rc = fan_get_status(&s);
8289 if (rc < 0)
8290 break;
8291
8292 /* Don't go out of emergency fan mode */
8293 if (s != 7) {
8294 s &= 0x07;
8295 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8296 }
8297
8298 if (!acpi_ec_write(fan_status_offset, s))
8299 rc = -EIO;
8300 else {
8301 tp_features.fan_ctrl_status_undef = 0;
8302 rc = 0;
8303 }
8304 break;
8305
8306 case TPACPI_FAN_WR_ACPI_SFAN:
8307 rc = fan_get_status(&s);
8308 if (rc < 0)
8309 break;
8310
8311 s &= 0x07;
8312
8313 /* Set fan to at least level 4 */
8314 s |= 4;
8315
8316 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008317 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008318 else
8319 rc = 0;
8320 break;
8321
8322 default:
8323 rc = -ENXIO;
8324 }
8325
8326 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008327
8328 if (!rc)
8329 vdbg_printk(TPACPI_DBG_FAN,
8330 "fan control: set fan control register to 0x%02x\n",
8331 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008332 return rc;
8333}
8334
8335static int fan_set_disable(void)
8336{
8337 int rc;
8338
8339 if (!fan_control_allowed)
8340 return -EPERM;
8341
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008342 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008343 return -ERESTARTSYS;
8344
8345 rc = 0;
8346 switch (fan_control_access_mode) {
8347 case TPACPI_FAN_WR_ACPI_FANS:
8348 case TPACPI_FAN_WR_TPEC:
8349 if (!acpi_ec_write(fan_status_offset, 0x00))
8350 rc = -EIO;
8351 else {
8352 fan_control_desired_level = 0;
8353 tp_features.fan_ctrl_status_undef = 0;
8354 }
8355 break;
8356
8357 case TPACPI_FAN_WR_ACPI_SFAN:
8358 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8359 rc = -EIO;
8360 else
8361 fan_control_desired_level = 0;
8362 break;
8363
8364 default:
8365 rc = -ENXIO;
8366 }
8367
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008368 if (!rc)
8369 vdbg_printk(TPACPI_DBG_FAN,
8370 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008371
8372 mutex_unlock(&fan_mutex);
8373 return rc;
8374}
8375
8376static int fan_set_speed(int speed)
8377{
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 rc = 0;
8387 switch (fan_control_access_mode) {
8388 case TPACPI_FAN_WR_ACPI_FANS:
8389 if (speed >= 0 && speed <= 65535) {
8390 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8391 speed, speed, speed))
8392 rc = -EIO;
8393 } else
8394 rc = -EINVAL;
8395 break;
8396
8397 default:
8398 rc = -ENXIO;
8399 }
8400
8401 mutex_unlock(&fan_mutex);
8402 return rc;
8403}
8404
8405static void fan_watchdog_reset(void)
8406{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008407 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8408 return;
8409
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008410 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008411 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8412 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8413 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8414 else
8415 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008416}
8417
8418static void fan_watchdog_fire(struct work_struct *ignored)
8419{
8420 int rc;
8421
8422 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8423 return;
8424
Joe Perches0978e012011-04-04 10:06:25 -07008425 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008426 rc = fan_set_enable();
8427 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008428 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8429 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008430 /* reschedule for later */
8431 fan_watchdog_reset();
8432 }
8433}
8434
8435/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008436 * SYSFS fan layout: hwmon compatible (device)
8437 *
8438 * pwm*_enable:
8439 * 0: "disengaged" mode
8440 * 1: manual mode
8441 * 2: native EC "auto" mode (recommended, hardware default)
8442 *
8443 * pwm*: set speed in manual mode, ignored otherwise.
8444 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8445 * interpolation.
8446 *
8447 * fan*_input: tachometer reading, RPM
8448 *
8449 *
8450 * SYSFS fan layout: extensions
8451 *
8452 * fan_watchdog (driver):
8453 * fan watchdog interval in seconds, 0 disables (default), max 120
8454 */
8455
8456/* sysfs fan pwm1_enable ----------------------------------------------- */
8457static ssize_t fan_pwm1_enable_show(struct device *dev,
8458 struct device_attribute *attr,
8459 char *buf)
8460{
8461 int res, mode;
8462 u8 status;
8463
8464 res = fan_get_status_safe(&status);
8465 if (res)
8466 return res;
8467
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008468 if (status & TP_EC_FAN_FULLSPEED) {
8469 mode = 0;
8470 } else if (status & TP_EC_FAN_AUTO) {
8471 mode = 2;
8472 } else
8473 mode = 1;
8474
Ye Guojin33ce79b2021-10-18 09:17:50 +00008475 return sysfs_emit(buf, "%d\n", mode);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008476}
8477
8478static ssize_t fan_pwm1_enable_store(struct device *dev,
8479 struct device_attribute *attr,
8480 const char *buf, size_t count)
8481{
8482 unsigned long t;
8483 int res, level;
8484
8485 if (parse_strtoul(buf, 2, &t))
8486 return -EINVAL;
8487
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008488 tpacpi_disclose_usertask("hwmon pwm1_enable",
8489 "set fan mode to %lu\n", t);
8490
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008491 switch (t) {
8492 case 0:
8493 level = TP_EC_FAN_FULLSPEED;
8494 break;
8495 case 1:
8496 level = TPACPI_FAN_LAST_LEVEL;
8497 break;
8498 case 2:
8499 level = TP_EC_FAN_AUTO;
8500 break;
8501 case 3:
8502 /* reserved for software-controlled auto mode */
8503 return -ENOSYS;
8504 default:
8505 return -EINVAL;
8506 }
8507
8508 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008509 if (res == -ENXIO)
8510 return -EINVAL;
8511 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008512 return res;
8513
8514 fan_watchdog_reset();
8515
8516 return count;
8517}
8518
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008519static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8520 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008521
8522/* sysfs fan pwm1 ------------------------------------------------------ */
8523static ssize_t fan_pwm1_show(struct device *dev,
8524 struct device_attribute *attr,
8525 char *buf)
8526{
8527 int res;
8528 u8 status;
8529
8530 res = fan_get_status_safe(&status);
8531 if (res)
8532 return res;
8533
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008534 if ((status &
8535 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8536 status = fan_control_desired_level;
8537
8538 if (status > 7)
8539 status = 7;
8540
Ye Guojin33ce79b2021-10-18 09:17:50 +00008541 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008542}
8543
8544static ssize_t fan_pwm1_store(struct device *dev,
8545 struct device_attribute *attr,
8546 const char *buf, size_t count)
8547{
8548 unsigned long s;
8549 int rc;
8550 u8 status, newlevel;
8551
8552 if (parse_strtoul(buf, 255, &s))
8553 return -EINVAL;
8554
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008555 tpacpi_disclose_usertask("hwmon pwm1",
8556 "set fan speed to %lu\n", s);
8557
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008558 /* scale down from 0-255 to 0-7 */
8559 newlevel = (s >> 5) & 0x07;
8560
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008561 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008562 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008563
8564 rc = fan_get_status(&status);
8565 if (!rc && (status &
8566 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8567 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008568 if (rc == -ENXIO)
8569 rc = -EINVAL;
8570 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008571 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008572 fan_watchdog_reset();
8573 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008574 }
8575
8576 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008577 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008578}
8579
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008580static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008581
8582/* sysfs fan fan1_input ------------------------------------------------ */
8583static ssize_t fan_fan1_input_show(struct device *dev,
8584 struct device_attribute *attr,
8585 char *buf)
8586{
8587 int res;
8588 unsigned int speed;
8589
8590 res = fan_get_speed(&speed);
8591 if (res < 0)
8592 return res;
8593
Ye Guojin33ce79b2021-10-18 09:17:50 +00008594 return sysfs_emit(buf, "%u\n", speed);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008595}
8596
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008597static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008598
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008599/* sysfs fan fan2_input ------------------------------------------------ */
8600static ssize_t fan_fan2_input_show(struct device *dev,
8601 struct device_attribute *attr,
8602 char *buf)
8603{
8604 int res;
8605 unsigned int speed;
8606
8607 res = fan2_get_speed(&speed);
8608 if (res < 0)
8609 return res;
8610
Ye Guojin33ce79b2021-10-18 09:17:50 +00008611 return sysfs_emit(buf, "%u\n", speed);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008612}
8613
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008614static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008615
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008616/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008617static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008618{
Ye Guojin33ce79b2021-10-18 09:17:50 +00008619 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008620}
8621
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008622static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8623 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008624{
8625 unsigned long t;
8626
8627 if (parse_strtoul(buf, 120, &t))
8628 return -EINVAL;
8629
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008630 if (!fan_control_allowed)
8631 return -EPERM;
8632
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008633 fan_watchdog_maxinterval = t;
8634 fan_watchdog_reset();
8635
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008636 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8637
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008638 return count;
8639}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008640static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008641
8642/* --------------------------------------------------------------------- */
Len Baker79f960e2021-10-23 17:40:36 +02008643
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008644static struct attribute *fan_attributes[] = {
Len Baker79f960e2021-10-23 17:40:36 +02008645 &dev_attr_pwm1_enable.attr,
8646 &dev_attr_pwm1.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008647 &dev_attr_fan1_input.attr,
Len Baker79f960e2021-10-23 17:40:36 +02008648 &dev_attr_fan2_input.attr,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008649 NULL
8650};
8651
Len Baker79f960e2021-10-23 17:40:36 +02008652static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
8653 int n)
8654{
8655 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8656 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8657 return 0;
8658
8659 if (attr == &dev_attr_fan2_input.attr) {
8660 if (!tp_features.second_fan)
8661 return 0;
8662 }
8663
8664 return attr->mode;
8665}
8666
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008667static const struct attribute_group fan_attr_group = {
Len Baker79f960e2021-10-23 17:40:36 +02008668 .is_visible = fan_attr_is_visible,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008669 .attrs = fan_attributes,
8670};
8671
Hans de Goede5cd68962021-11-23 22:04:21 +01008672static struct attribute *fan_driver_attributes[] = {
8673 &driver_attr_fan_watchdog.attr,
8674 NULL
8675};
8676
8677static const struct attribute_group fan_driver_attr_group = {
8678 .is_visible = fan_attr_is_visible,
8679 .attrs = fan_driver_attributes,
8680};
8681
Jouke Witteveen599eeff2018-12-05 12:17:52 +01008682#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008683#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Lars Hofhansl14232c62020-04-23 14:57:09 -07008684#define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008685
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008686static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
Jouke Witteveen599eeff2018-12-05 12:17:52 +01008687 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8688 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8689 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8690 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8691 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8692 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
Lars Hofhansl14232c62020-04-23 14:57:09 -07008693 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8694 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8695 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8696 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8697 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
Jeannie Stevensonc47c0422021-01-15 16:06:30 +00008698 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
Lars Hofhansl14232c62020-04-23 14:57:09 -07008699 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8700 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
Iakov 'Jake' Kirilenkoc758be82020-11-05 18:25:56 +03008701 TPACPI_Q_LNV3('N', '2', 'V', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (3nd gen) */
Jimmy Wang1f338952021-11-05 17:05:28 +08008702 TPACPI_Q_LNV3('N', '4', '0', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (4nd gen) */
Matthias Maiere8637df2020-11-25 18:04:16 -06008703 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
Til Jasper Ullrich25acf212021-05-25 17:09:52 +02008704 TPACPI_Q_LNV3('N', '3', '2', TPACPI_FAN_2CTL), /* X1 Carbon (9th gen) */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008705};
8706
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008707static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008708{
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008709 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008710
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008711 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8712 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008713
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008714 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008715 fan_status_access_mode = TPACPI_FAN_NONE;
8716 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008717 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008718 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008719 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008720 tp_features.second_fan = 0;
Lars Hofhansl14232c62020-04-23 14:57:09 -07008721 tp_features.second_fan_ctl = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008722 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008723
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008724 if (tpacpi_is_ibm()) {
8725 TPACPI_ACPIHANDLE_INIT(fans);
8726 TPACPI_ACPIHANDLE_INIT(gfan);
8727 TPACPI_ACPIHANDLE_INIT(sfan);
8728 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008729
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008730 quirks = tpacpi_check_quirks(fan_quirk_table,
8731 ARRAY_SIZE(fan_quirk_table));
8732
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008733 if (gfan_handle) {
8734 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008735 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008736 } else {
8737 /* all other ThinkPads: note that even old-style
8738 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008739 if (likely(acpi_ec_read(fan_status_offset,
8740 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008741 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008742 if (quirks & TPACPI_FAN_Q1)
8743 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008744 if (quirks & TPACPI_FAN_2FAN) {
8745 tp_features.second_fan = 1;
Lars Hofhansl14232c62020-04-23 14:57:09 -07008746 pr_info("secondary fan support enabled\n");
8747 }
8748 if (quirks & TPACPI_FAN_2CTL) {
8749 tp_features.second_fan = 1;
8750 tp_features.second_fan_ctl = 1;
8751 pr_info("secondary fan control enabled\n");
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008752 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008753 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008754 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Hans de Goedec7e1c782021-11-21 20:11:24 +01008755 return -ENODEV;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008756 }
8757 }
8758
8759 if (sfan_handle) {
8760 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008761 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008762 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008763 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008764 } else {
8765 if (!gfan_handle) {
8766 /* gfan without sfan means no fan control */
8767 /* all other models implement TP EC 0x2f control */
8768
8769 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008770 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008771 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008772 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008773 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008774 TPACPI_FAN_CMD_SPEED |
8775 TPACPI_FAN_CMD_LEVEL |
8776 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008777 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008778 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008779 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008780 TPACPI_FAN_CMD_LEVEL |
8781 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008782 }
8783 }
8784 }
8785
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008786 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8787 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008788 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8789 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8790 fan_status_access_mode, fan_control_access_mode);
8791
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008792 /* fan control master switch */
8793 if (!fan_control_allowed) {
8794 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8795 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008796 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008797 "fan control features disabled by parameter\n");
8798 }
8799
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008800 /* update fan_control_desired_level */
8801 if (fan_status_access_mode != TPACPI_FAN_NONE)
8802 fan_get_status_safe(NULL);
8803
Hans de Goedec7e1c782021-11-21 20:11:24 +01008804 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8805 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8806 return -ENODEV;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008807
Hans de Goedec7e1c782021-11-21 20:11:24 +01008808 return 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008809}
8810
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008811static void fan_exit(void)
8812{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008813 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008814 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008815
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008816 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008817 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008818}
8819
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008820static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008821{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008822 int rc;
8823
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008824 if (!fan_control_allowed)
8825 return;
8826
8827 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008828 fan_control_resume_level = 0;
8829 rc = fan_get_status_safe(&fan_control_resume_level);
8830 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008831 pr_notice("failed to read fan level for later restore during resume: %d\n",
8832 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008833
8834 /* if it is undefined, don't attempt to restore it.
8835 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008836 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008837 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008838}
8839
8840static void fan_resume(void)
8841{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008842 u8 current_level = 7;
8843 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008844 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008845
8846 /* DSDT *always* updates status on resume */
8847 tp_features.fan_ctrl_status_undef = 0;
8848
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008849 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008850 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008851 (fan_get_status_safe(&current_level) < 0))
8852 return;
8853
8854 switch (fan_control_access_mode) {
8855 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008856 /* never decrease fan level */
8857 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008858 break;
8859 case TPACPI_FAN_WR_ACPI_FANS:
8860 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008861 /* never decrease fan level, scale is:
8862 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8863 *
8864 * We expect the firmware to set either 7 or AUTO, but we
8865 * handle FULLSPEED out of paranoia.
8866 *
8867 * So, we can safely only restore FULLSPEED or 7, anything
8868 * else could slow the fan. Restoring AUTO is useless, at
8869 * best that's exactly what the DSDT already set (it is the
8870 * slower it uses).
8871 *
8872 * Always keep in mind that the DSDT *will* have set the
8873 * fans to what the vendor supposes is the best level. We
8874 * muck with it only to speed the fan up.
8875 */
8876 if (fan_control_resume_level != 7 &&
8877 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8878 return;
8879 else
8880 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8881 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008882 break;
8883 default:
8884 return;
8885 }
8886 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008887 pr_notice("restoring fan level to 0x%02x\n",
8888 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008889 rc = fan_set_level_safe(fan_control_resume_level);
8890 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008891 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008892 }
8893}
8894
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008895static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008896{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008897 int rc;
8898 u8 status;
8899 unsigned int speed = 0;
8900
8901 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008902 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008903 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008904 rc = fan_get_status_safe(&status);
8905 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008906 return rc;
8907
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008908 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008909 "level:\t\t%d\n",
8910 (status != 0) ? "enabled" : "disabled", status);
8911 break;
8912
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008913 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008914 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008915 rc = fan_get_status_safe(&status);
8916 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008917 return rc;
8918
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008919 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008920 (status != 0) ? "enabled" : "disabled");
8921
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008922 rc = fan_get_speed(&speed);
8923 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008924 return rc;
8925
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008926 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008927
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008928 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008929 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008930 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008931 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008932 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008933 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008934 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008935 break;
8936
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008937 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008938 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008939 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008940 }
8941
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008942 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008943 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008944
8945 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008946 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008947 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008948 break;
8949
8950 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008951 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008952 break;
8953 }
8954 }
8955
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008956 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008957 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008958 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008959
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008960 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008961 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008962
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008963 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008964}
8965
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008966static int fan_write_cmd_level(const char *cmd, int *rc)
8967{
8968 int level;
8969
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008970 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008971 level = TP_EC_FAN_AUTO;
Nathan Chancellorfd96e352021-10-18 11:25:37 -07008972 else if ((strlencmp(cmd, "level disengaged") == 0) ||
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008973 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008974 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008975 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008976 return 0;
8977
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008978 *rc = fan_set_level_safe(level);
8979 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008980 pr_err("level command accepted for unsupported access mode %d\n",
8981 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008982 else if (!*rc)
8983 tpacpi_disclose_usertask("procfs fan",
8984 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008985
8986 return 1;
8987}
8988
8989static int fan_write_cmd_enable(const char *cmd, int *rc)
8990{
8991 if (strlencmp(cmd, "enable") != 0)
8992 return 0;
8993
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008994 *rc = fan_set_enable();
8995 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008996 pr_err("enable command accepted for unsupported access mode %d\n",
8997 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008998 else if (!*rc)
8999 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009000
9001 return 1;
9002}
9003
9004static int fan_write_cmd_disable(const char *cmd, int *rc)
9005{
9006 if (strlencmp(cmd, "disable") != 0)
9007 return 0;
9008
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009009 *rc = fan_set_disable();
9010 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009011 pr_err("disable command accepted for unsupported access mode %d\n",
9012 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009013 else if (!*rc)
9014 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009015
9016 return 1;
9017}
9018
9019static int fan_write_cmd_speed(const char *cmd, int *rc)
9020{
9021 int speed;
9022
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009023 /* TODO:
9024 * Support speed <low> <medium> <high> ? */
9025
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009026 if (sscanf(cmd, "speed %d", &speed) != 1)
9027 return 0;
9028
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009029 *rc = fan_set_speed(speed);
9030 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009031 pr_err("speed command accepted for unsupported access mode %d\n",
9032 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009033 else if (!*rc)
9034 tpacpi_disclose_usertask("procfs fan",
9035 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009036
9037 return 1;
9038}
9039
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009040static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9041{
9042 int interval;
9043
9044 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9045 return 0;
9046
9047 if (interval < 0 || interval > 120)
9048 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009049 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009050 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009051 tpacpi_disclose_usertask("procfs fan",
9052 "set watchdog timer to %d\n",
9053 interval);
9054 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009055
9056 return 1;
9057}
9058
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009059static int fan_write(char *buf)
9060{
9061 char *cmd;
9062 int rc = 0;
9063
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03009064 while (!rc && (cmd = strsep(&buf, ","))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009065 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009066 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009067 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009068 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009069 fan_write_cmd_disable(cmd, &rc) ||
9070 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009071 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009072 fan_write_cmd_speed(cmd, &rc))
9073 )
9074 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009075 else if (!rc)
9076 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009077 }
9078
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009079 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009080}
9081
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009082static struct ibm_struct fan_driver_data = {
9083 .name = "fan",
9084 .read = fan_read,
9085 .write = fan_write,
9086 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009087 .suspend = fan_suspend,
9088 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009089};
9090
David Henningsson420f9732013-10-16 23:10:31 +02009091/*************************************************************************
9092 * Mute LED subdriver
9093 */
9094
Takashi Iwai9e908a12018-11-26 17:47:47 +01009095#define TPACPI_LED_MAX 2
David Henningsson420f9732013-10-16 23:10:31 +02009096
9097struct tp_led_table {
9098 acpi_string name;
9099 int on_value;
9100 int off_value;
9101 int state;
9102};
9103
Takashi Iwai9e908a12018-11-26 17:47:47 +01009104static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9105 [LED_AUDIO_MUTE] = {
David Henningsson420f9732013-10-16 23:10:31 +02009106 .name = "SSMS",
9107 .on_value = 1,
9108 .off_value = 0,
9109 },
Takashi Iwai9e908a12018-11-26 17:47:47 +01009110 [LED_AUDIO_MICMUTE] = {
David Henningsson420f9732013-10-16 23:10:31 +02009111 .name = "MMTS",
9112 .on_value = 2,
9113 .off_value = 0,
9114 },
9115};
9116
9117static int mute_led_on_off(struct tp_led_table *t, bool state)
9118{
9119 acpi_handle temp;
9120 int output;
9121
Axel Lin3526eca2016-09-19 09:33:51 +08009122 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009123 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9124 return -EIO;
9125 }
9126
9127 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9128 state ? t->on_value : t->off_value))
9129 return -EIO;
9130
9131 t->state = state;
9132 return state;
9133}
9134
Takashi Iwai9e908a12018-11-26 17:47:47 +01009135static int tpacpi_led_set(int whichled, bool on)
David Henningsson420f9732013-10-16 23:10:31 +02009136{
9137 struct tp_led_table *t;
9138
David Henningsson420f9732013-10-16 23:10:31 +02009139 t = &led_tables[whichled];
9140 if (t->state < 0 || t->state == on)
9141 return t->state;
9142 return mute_led_on_off(t, on);
9143}
David Henningsson420f9732013-10-16 23:10:31 +02009144
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009145static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9146 enum led_brightness brightness)
9147{
Takashi Iwai9e908a12018-11-26 17:47:47 +01009148 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009149}
9150
9151static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9152 enum led_brightness brightness)
9153{
Takashi Iwai9e908a12018-11-26 17:47:47 +01009154 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009155}
9156
Takashi Iwai9e908a12018-11-26 17:47:47 +01009157static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9158 [LED_AUDIO_MUTE] = {
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009159 .name = "platform::mute",
9160 .max_brightness = 1,
9161 .brightness_set_blocking = tpacpi_led_mute_set,
9162 .default_trigger = "audio-mute",
9163 },
Takashi Iwai9e908a12018-11-26 17:47:47 +01009164 [LED_AUDIO_MICMUTE] = {
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009165 .name = "platform::micmute",
9166 .max_brightness = 1,
9167 .brightness_set_blocking = tpacpi_led_micmute_set,
9168 .default_trigger = "audio-micmute",
9169 },
9170};
David Henningsson420f9732013-10-16 23:10:31 +02009171
9172static int mute_led_init(struct ibm_init_struct *iibm)
9173{
9174 acpi_handle temp;
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009175 int i, err;
David Henningsson420f9732013-10-16 23:10:31 +02009176
9177 for (i = 0; i < TPACPI_LED_MAX; i++) {
9178 struct tp_led_table *t = &led_tables[i];
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009179 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009180 t->state = -ENODEV;
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009181 continue;
9182 }
9183
Takashi Iwai9e908a12018-11-26 17:47:47 +01009184 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009185 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9186 if (err < 0) {
Andy Shevchenko41f80042020-05-07 19:37:23 +03009187 while (i--)
9188 led_classdev_unregister(&mute_led_cdev[i]);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009189 return err;
9190 }
David Henningsson420f9732013-10-16 23:10:31 +02009191 }
9192 return 0;
9193}
9194
9195static void mute_led_exit(void)
9196{
9197 int i;
9198
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009199 for (i = 0; i < TPACPI_LED_MAX; i++) {
Andy Shevchenko41f80042020-05-07 19:37:23 +03009200 led_classdev_unregister(&mute_led_cdev[i]);
9201 tpacpi_led_set(i, false);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009202 }
David Henningsson420f9732013-10-16 23:10:31 +02009203}
9204
Takashi Iwai119f4492014-02-12 16:32:45 +01009205static void mute_led_resume(void)
9206{
9207 int i;
9208
9209 for (i = 0; i < TPACPI_LED_MAX; i++) {
9210 struct tp_led_table *t = &led_tables[i];
9211 if (t->state >= 0)
9212 mute_led_on_off(t, t->state);
9213 }
9214}
9215
David Henningsson420f9732013-10-16 23:10:31 +02009216static struct ibm_struct mute_led_driver_data = {
9217 .name = "mute_led",
9218 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009219 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009220};
9221
Ognjen Galic2801b962018-02-07 15:58:44 +01009222/*
9223 * Battery Wear Control Driver
9224 * Contact: Ognjen Galic <smclt30p@gmail.com>
9225 */
9226
9227/* Metadata */
9228
9229#define GET_START "BCTG"
9230#define SET_START "BCCS"
9231#define GET_STOP "BCSG"
9232#define SET_STOP "BCSS"
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009233#define GET_DISCHARGE "BDSG"
9234#define SET_DISCHARGE "BDSS"
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009235#define GET_INHIBIT "BICG"
9236#define SET_INHIBIT "BICS"
Ognjen Galic2801b962018-02-07 15:58:44 +01009237
Ognjen Galic2801b962018-02-07 15:58:44 +01009238enum {
9239 BAT_ANY = 0,
9240 BAT_PRIMARY = 1,
9241 BAT_SECONDARY = 2
9242};
9243
9244enum {
9245 /* Error condition bit */
9246 METHOD_ERR = BIT(31),
9247};
9248
9249enum {
9250 /* This is used in the get/set helpers */
9251 THRESHOLD_START,
9252 THRESHOLD_STOP,
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009253 FORCE_DISCHARGE,
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009254 INHIBIT_CHARGE,
Ognjen Galic2801b962018-02-07 15:58:44 +01009255};
9256
9257struct tpacpi_battery_data {
9258 int charge_start;
9259 int start_support;
9260 int charge_stop;
9261 int stop_support;
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009262 unsigned int charge_behaviours;
Ognjen Galic2801b962018-02-07 15:58:44 +01009263};
9264
9265struct tpacpi_battery_driver_data {
9266 struct tpacpi_battery_data batteries[3];
9267 int individual_addressing;
9268};
9269
9270static struct tpacpi_battery_driver_data battery_info;
9271
9272/* ACPI helpers/functions/probes */
9273
9274/**
9275 * This evaluates a ACPI method call specific to the battery
9276 * ACPI extension. The specifics are that an error is marked
9277 * in the 32rd bit of the response, so we just check that here.
9278 */
9279static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9280{
9281 int response;
9282
9283 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9284 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9285 return AE_ERROR;
9286 }
9287 if (response & METHOD_ERR) {
9288 acpi_handle_err(hkey_handle,
9289 "%s evaluated but flagged as error", method);
9290 return AE_ERROR;
9291 }
9292 *ret = response;
9293 return AE_OK;
9294}
9295
9296static int tpacpi_battery_get(int what, int battery, int *ret)
9297{
9298 switch (what) {
9299 case THRESHOLD_START:
9300 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9301 return -ENODEV;
9302
9303 /* The value is in the low 8 bits of the response */
9304 *ret = *ret & 0xFF;
9305 return 0;
9306 case THRESHOLD_STOP:
9307 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9308 return -ENODEV;
9309 /* Value is in lower 8 bits */
9310 *ret = *ret & 0xFF;
9311 /*
9312 * On the stop value, if we return 0 that
9313 * does not make any sense. 0 means Default, which
9314 * means that charging stops at 100%, so we return
9315 * that.
9316 */
9317 if (*ret == 0)
9318 *ret = 100;
9319 return 0;
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009320 case FORCE_DISCHARGE:
9321 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
9322 return -ENODEV;
9323 /* The force discharge status is in bit 0 */
9324 *ret = *ret & 0x01;
9325 return 0;
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009326 case INHIBIT_CHARGE:
9327 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
9328 return -ENODEV;
9329 /* The inhibit charge status is in bit 0 */
9330 *ret = *ret & 0x01;
9331 return 0;
Ognjen Galic2801b962018-02-07 15:58:44 +01009332 default:
9333 pr_crit("wrong parameter: %d", what);
9334 return -EINVAL;
9335 }
9336}
9337
9338static int tpacpi_battery_set(int what, int battery, int value)
9339{
9340 int param, ret;
9341 /* The first 8 bits are the value of the threshold */
9342 param = value;
9343 /* The battery ID is in bits 8-9, 2 bits */
9344 param |= battery << 8;
9345
9346 switch (what) {
9347 case THRESHOLD_START:
9348 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9349 pr_err("failed to set charge threshold on battery %d",
9350 battery);
9351 return -ENODEV;
9352 }
9353 return 0;
9354 case THRESHOLD_STOP:
9355 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9356 pr_err("failed to set stop threshold: %d", battery);
9357 return -ENODEV;
9358 }
9359 return 0;
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009360 case FORCE_DISCHARGE:
9361 /* Force discharge is in bit 0,
9362 * break on AC attach is in bit 1 (won't work on some ThinkPads),
9363 * battery ID is in bits 8-9, 2 bits.
9364 */
9365 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
9366 pr_err("failed to set force discharge on %d", battery);
9367 return -ENODEV;
9368 }
9369 return 0;
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009370 case INHIBIT_CHARGE:
9371 /* When setting inhibit charge, we set a default value of
9372 * always breaking on AC detach and the effective time is set to
9373 * be permanent.
9374 * The battery ID is in bits 4-5, 2 bits,
9375 * the effective time is in bits 8-23, 2 bytes.
9376 * A time of FFFF indicates forever.
9377 */
9378 param = value;
9379 param |= battery << 4;
9380 param |= 0xFFFF << 8;
9381 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
9382 pr_err("failed to set inhibit charge on %d", battery);
9383 return -ENODEV;
9384 }
9385 return 0;
Ognjen Galic2801b962018-02-07 15:58:44 +01009386 default:
9387 pr_crit("wrong parameter: %d", what);
9388 return -EINVAL;
9389 }
9390}
9391
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009392static int tpacpi_battery_set_validate(int what, int battery, int value)
9393{
9394 int ret, v;
9395
9396 ret = tpacpi_battery_set(what, battery, value);
9397 if (ret < 0)
9398 return ret;
9399
9400 ret = tpacpi_battery_get(what, battery, &v);
9401 if (ret < 0)
9402 return ret;
9403
9404 if (v == value)
9405 return 0;
9406
9407 msleep(500);
9408
9409 ret = tpacpi_battery_get(what, battery, &v);
9410 if (ret < 0)
9411 return ret;
9412
9413 if (v == value)
9414 return 0;
9415
9416 return -EIO;
9417}
9418
Ognjen Galic2801b962018-02-07 15:58:44 +01009419static int tpacpi_battery_probe(int battery)
9420{
9421 int ret = 0;
9422
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009423 memset(&battery_info.batteries[battery], 0,
9424 sizeof(battery_info.batteries[battery]));
9425
Ognjen Galic2801b962018-02-07 15:58:44 +01009426 /*
9427 * 1) Get the current start threshold
9428 * 2) Check for support
9429 * 3) Get the current stop threshold
9430 * 4) Check for support
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009431 * 5) Get the current force discharge status
9432 * 6) Check for support
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009433 * 7) Get the current inhibit charge status
9434 * 8) Check for support
Ognjen Galic2801b962018-02-07 15:58:44 +01009435 */
9436 if (acpi_has_method(hkey_handle, GET_START)) {
9437 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9438 pr_err("Error probing battery %d\n", battery);
9439 return -ENODEV;
9440 }
9441 /* Individual addressing is in bit 9 */
9442 if (ret & BIT(9))
9443 battery_info.individual_addressing = true;
9444 /* Support is marked in bit 8 */
9445 if (ret & BIT(8))
9446 battery_info.batteries[battery].start_support = 1;
9447 else
9448 return -ENODEV;
9449 if (tpacpi_battery_get(THRESHOLD_START, battery,
9450 &battery_info.batteries[battery].charge_start)) {
9451 pr_err("Error probing battery %d\n", battery);
9452 return -ENODEV;
9453 }
9454 }
9455 if (acpi_has_method(hkey_handle, GET_STOP)) {
9456 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9457 pr_err("Error probing battery stop; %d\n", battery);
9458 return -ENODEV;
9459 }
9460 /* Support is marked in bit 8 */
9461 if (ret & BIT(8))
9462 battery_info.batteries[battery].stop_support = 1;
9463 else
9464 return -ENODEV;
9465 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9466 &battery_info.batteries[battery].charge_stop)) {
9467 pr_err("Error probing battery stop: %d\n", battery);
9468 return -ENODEV;
9469 }
9470 }
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009471 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
9472 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
9473 pr_err("Error probing battery discharge; %d\n", battery);
9474 return -ENODEV;
9475 }
9476 /* Support is marked in bit 8 */
9477 if (ret & BIT(8))
9478 battery_info.batteries[battery].charge_behaviours |=
9479 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
9480 }
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009481 if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
9482 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
9483 pr_err("Error probing battery inhibit charge; %d\n", battery);
9484 return -ENODEV;
9485 }
9486 /* Support is marked in bit 5 */
9487 if (ret & BIT(5))
9488 battery_info.batteries[battery].charge_behaviours |=
9489 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
9490 }
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009491
9492 battery_info.batteries[battery].charge_behaviours |=
9493 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
9494
9495 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
9496 battery,
9497 battery_info.batteries[battery].charge_start,
9498 battery_info.batteries[battery].charge_stop,
9499 battery_info.batteries[battery].charge_behaviours);
Ognjen Galic2801b962018-02-07 15:58:44 +01009500
9501 return 0;
9502}
9503
9504/* General helper functions */
9505
9506static int tpacpi_battery_get_id(const char *battery_name)
9507{
9508
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009509 if (strcmp(battery_name, "BAT0") == 0 ||
9510 tp_features.battery_force_primary)
Ognjen Galic2801b962018-02-07 15:58:44 +01009511 return BAT_PRIMARY;
9512 if (strcmp(battery_name, "BAT1") == 0)
9513 return BAT_SECONDARY;
9514 /*
9515 * If for some reason the battery is not BAT0 nor is it
9516 * BAT1, we will assume it's the default, first battery,
9517 * AKA primary.
9518 */
9519 pr_warn("unknown battery %s, assuming primary", battery_name);
9520 return BAT_PRIMARY;
9521}
9522
9523/* sysfs interface */
9524
9525static ssize_t tpacpi_battery_store(int what,
9526 struct device *dev,
9527 const char *buf, size_t count)
9528{
9529 struct power_supply *supply = to_power_supply(dev);
9530 unsigned long value;
9531 int battery, rval;
9532 /*
9533 * Some systems have support for more than
9534 * one battery. If that is the case,
9535 * tpacpi_battery_probe marked that addressing
9536 * them individually is supported, so we do that
9537 * based on the device struct.
9538 *
9539 * On systems that are not supported, we assume
9540 * the primary as most of the ACPI calls fail
9541 * with "Any Battery" as the parameter.
9542 */
9543 if (battery_info.individual_addressing)
9544 /* BAT_PRIMARY or BAT_SECONDARY */
9545 battery = tpacpi_battery_get_id(supply->desc->name);
9546 else
9547 battery = BAT_PRIMARY;
9548
9549 rval = kstrtoul(buf, 10, &value);
9550 if (rval)
9551 return rval;
9552
9553 switch (what) {
9554 case THRESHOLD_START:
9555 if (!battery_info.batteries[battery].start_support)
9556 return -ENODEV;
9557 /* valid values are [0, 99] */
Xiongfeng Wangb9911782020-04-29 16:59:40 +08009558 if (value > 99)
Ognjen Galic2801b962018-02-07 15:58:44 +01009559 return -EINVAL;
9560 if (value > battery_info.batteries[battery].charge_stop)
9561 return -EINVAL;
9562 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9563 return -ENODEV;
9564 battery_info.batteries[battery].charge_start = value;
9565 return count;
9566
9567 case THRESHOLD_STOP:
9568 if (!battery_info.batteries[battery].stop_support)
9569 return -ENODEV;
9570 /* valid values are [1, 100] */
9571 if (value < 1 || value > 100)
9572 return -EINVAL;
9573 if (value < battery_info.batteries[battery].charge_start)
9574 return -EINVAL;
9575 battery_info.batteries[battery].charge_stop = value;
9576 /*
9577 * When 100 is passed to stop, we need to flip
9578 * it to 0 as that the EC understands that as
9579 * "Default", which will charge to 100%
9580 */
9581 if (value == 100)
9582 value = 0;
9583 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9584 return -EINVAL;
9585 return count;
9586 default:
9587 pr_crit("Wrong parameter: %d", what);
9588 return -EINVAL;
9589 }
9590 return count;
9591}
9592
9593static ssize_t tpacpi_battery_show(int what,
9594 struct device *dev,
9595 char *buf)
9596{
9597 struct power_supply *supply = to_power_supply(dev);
9598 int ret, battery;
9599 /*
9600 * Some systems have support for more than
9601 * one battery. If that is the case,
9602 * tpacpi_battery_probe marked that addressing
9603 * them individually is supported, so we;
9604 * based on the device struct.
9605 *
9606 * On systems that are not supported, we assume
9607 * the primary as most of the ACPI calls fail
9608 * with "Any Battery" as the parameter.
9609 */
9610 if (battery_info.individual_addressing)
9611 /* BAT_PRIMARY or BAT_SECONDARY */
9612 battery = tpacpi_battery_get_id(supply->desc->name);
9613 else
9614 battery = BAT_PRIMARY;
9615 if (tpacpi_battery_get(what, battery, &ret))
9616 return -ENODEV;
9617 return sprintf(buf, "%d\n", ret);
9618}
9619
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009620static ssize_t charge_control_start_threshold_show(struct device *device,
Ognjen Galic2801b962018-02-07 15:58:44 +01009621 struct device_attribute *attr,
9622 char *buf)
9623{
9624 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9625}
9626
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009627static ssize_t charge_control_end_threshold_show(struct device *device,
Ognjen Galic2801b962018-02-07 15:58:44 +01009628 struct device_attribute *attr,
9629 char *buf)
9630{
9631 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9632}
9633
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009634static ssize_t charge_behaviour_show(struct device *dev,
9635 struct device_attribute *attr,
9636 char *buf)
9637{
9638 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
9639 struct power_supply *supply = to_power_supply(dev);
9640 unsigned int available;
9641 int ret, battery;
9642
9643 battery = tpacpi_battery_get_id(supply->desc->name);
9644 available = battery_info.batteries[battery].charge_behaviours;
9645
9646 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
9647 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
9648 return -ENODEV;
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009649 if (ret) {
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009650 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009651 goto out;
9652 }
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009653 }
9654
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009655 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
9656 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
9657 return -ENODEV;
9658 if (ret) {
9659 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
9660 goto out;
9661 }
9662 }
9663
9664out:
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009665 return power_supply_charge_behaviour_show(dev, available, active, buf);
9666}
9667
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009668static ssize_t charge_control_start_threshold_store(struct device *dev,
Ognjen Galic2801b962018-02-07 15:58:44 +01009669 struct device_attribute *attr,
9670 const char *buf, size_t count)
9671{
9672 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9673}
9674
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009675static ssize_t charge_control_end_threshold_store(struct device *dev,
Ognjen Galic2801b962018-02-07 15:58:44 +01009676 struct device_attribute *attr,
9677 const char *buf, size_t count)
9678{
9679 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9680}
9681
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009682static ssize_t charge_behaviour_store(struct device *dev,
9683 struct device_attribute *attr,
9684 const char *buf, size_t count)
9685{
9686 struct power_supply *supply = to_power_supply(dev);
9687 int selected, battery, ret = 0;
9688 unsigned int available;
9689
9690 battery = tpacpi_battery_get_id(supply->desc->name);
9691 available = battery_info.batteries[battery].charge_behaviours;
9692 selected = power_supply_charge_behaviour_parse(available, buf);
9693
9694 if (selected < 0)
9695 return selected;
9696
9697 switch (selected) {
9698 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
9699 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9700 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009701 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9702 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009703 if (ret < 0)
9704 return ret;
9705 break;
9706 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
Thomas Weißschuh400cffd2021-11-24 00:27:04 +01009707 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9708 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
9709 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
9710 if (ret < 0)
9711 return ret;
9712 break;
9713 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
9714 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9715 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9716 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009717 if (ret < 0)
9718 return ret;
9719 break;
9720 default:
9721 dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
9722 return -EINVAL;
9723 }
9724
9725 return count;
9726}
9727
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009728static DEVICE_ATTR_RW(charge_control_start_threshold);
9729static DEVICE_ATTR_RW(charge_control_end_threshold);
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009730static DEVICE_ATTR_RW(charge_behaviour);
Wei Yongjun066d7f32020-07-29 23:59:14 +08009731static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009732 charge_start_threshold,
9733 0644,
9734 charge_control_start_threshold_show,
9735 charge_control_start_threshold_store
9736);
Wei Yongjun066d7f32020-07-29 23:59:14 +08009737static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009738 charge_stop_threshold,
9739 0644,
9740 charge_control_end_threshold_show,
9741 charge_control_end_threshold_store
9742);
Ognjen Galic2801b962018-02-07 15:58:44 +01009743
9744static struct attribute *tpacpi_battery_attrs[] = {
Thomas Weißschuhe3392952020-02-03 13:01:32 +01009745 &dev_attr_charge_control_start_threshold.attr,
9746 &dev_attr_charge_control_end_threshold.attr,
Ognjen Galic2801b962018-02-07 15:58:44 +01009747 &dev_attr_charge_start_threshold.attr,
9748 &dev_attr_charge_stop_threshold.attr,
Thomas Weißschuhb55d4162021-11-24 00:27:03 +01009749 &dev_attr_charge_behaviour.attr,
Ognjen Galic2801b962018-02-07 15:58:44 +01009750 NULL,
9751};
9752
9753ATTRIBUTE_GROUPS(tpacpi_battery);
9754
9755/* ACPI battery hooking */
9756
9757static int tpacpi_battery_add(struct power_supply *battery)
9758{
9759 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9760
9761 if (tpacpi_battery_probe(batteryid))
9762 return -ENODEV;
9763 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9764 return -ENODEV;
9765 return 0;
9766}
9767
9768static int tpacpi_battery_remove(struct power_supply *battery)
9769{
9770 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9771 return 0;
9772}
9773
9774static struct acpi_battery_hook battery_hook = {
9775 .add_battery = tpacpi_battery_add,
9776 .remove_battery = tpacpi_battery_remove,
9777 .name = "ThinkPad Battery Extension",
9778};
9779
9780/* Subdriver init/exit */
9781
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009782static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9783 /*
9784 * Individual addressing is broken on models that expose the
9785 * primary battery as BAT1.
9786 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009787 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9788 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9789 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009790 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009791 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
Hans de Goede3cd420b2020-11-09 11:35:50 +01009792 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009793};
9794
Ognjen Galic2801b962018-02-07 15:58:44 +01009795static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9796{
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009797 memset(&battery_info, 0, sizeof(battery_info));
9798
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009799 tp_features.battery_force_primary = tpacpi_check_quirks(
9800 battery_quirk_table,
9801 ARRAY_SIZE(battery_quirk_table));
9802
Ognjen Galic2801b962018-02-07 15:58:44 +01009803 battery_hook_register(&battery_hook);
9804 return 0;
9805}
9806
9807static void tpacpi_battery_exit(void)
9808{
9809 battery_hook_unregister(&battery_hook);
9810}
9811
9812static struct ibm_struct battery_driver_data = {
9813 .name = "battery",
9814 .exit = tpacpi_battery_exit,
9815};
9816
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009817/*************************************************************************
9818 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9819 */
9820
Hans de Goedef8098912021-10-05 22:23:20 +02009821static struct drm_privacy_screen *lcdshadow_dev;
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009822static acpi_handle lcdshadow_get_handle;
9823static acpi_handle lcdshadow_set_handle;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009824
Hans de Goedef8098912021-10-05 22:23:20 +02009825static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
9826 enum drm_privacy_screen_status state)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009827{
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009828 int output;
9829
Hans de Goedef8098912021-10-05 22:23:20 +02009830 if (WARN_ON(!mutex_is_locked(&priv->lock)))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009831 return -EIO;
9832
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009833 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009834 return -EIO;
9835
Hans de Goedef8098912021-10-05 22:23:20 +02009836 priv->hw_state = priv->sw_state = state;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009837 return 0;
9838}
9839
Hans de Goedef8098912021-10-05 22:23:20 +02009840static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009841{
Hans de Goedef8098912021-10-05 22:23:20 +02009842 int output;
9843
9844 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
9845 return;
9846
9847 priv->hw_state = priv->sw_state = output & 0x1;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009848}
9849
Hans de Goedef8098912021-10-05 22:23:20 +02009850static const struct drm_privacy_screen_ops lcdshadow_ops = {
9851 .set_sw_state = lcdshadow_set_sw_state,
9852 .get_hw_state = lcdshadow_get_hw_state,
9853};
9854
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009855static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9856{
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009857 acpi_status status1, status2;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009858 int output;
9859
Hans de Goedee8b7eb62021-10-05 22:23:19 +02009860 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
9861 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
Hans de Goedef8098912021-10-05 22:23:20 +02009862 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009863 return 0;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009864
Hans de Goedef8098912021-10-05 22:23:20 +02009865 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009866 return -EIO;
Hans de Goedef8098912021-10-05 22:23:20 +02009867
9868 if (!(output & 0x10000))
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009869 return 0;
Hans de Goedef8098912021-10-05 22:23:20 +02009870
9871 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
9872 &lcdshadow_ops);
9873 if (IS_ERR(lcdshadow_dev))
9874 return PTR_ERR(lcdshadow_dev);
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009875
9876 return 0;
9877}
9878
Hans de Goedef8098912021-10-05 22:23:20 +02009879static void lcdshadow_exit(void)
9880{
9881 drm_privacy_screen_unregister(lcdshadow_dev);
9882}
9883
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009884static void lcdshadow_resume(void)
9885{
Hans de Goedef8098912021-10-05 22:23:20 +02009886 if (!lcdshadow_dev)
9887 return;
9888
9889 mutex_lock(&lcdshadow_dev->lock);
9890 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
9891 mutex_unlock(&lcdshadow_dev->lock);
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009892}
9893
9894static int lcdshadow_read(struct seq_file *m)
9895{
Hans de Goedef8098912021-10-05 22:23:20 +02009896 if (!lcdshadow_dev) {
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009897 seq_puts(m, "status:\t\tnot supported\n");
9898 } else {
Hans de Goedef8098912021-10-05 22:23:20 +02009899 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009900 seq_puts(m, "commands:\t0, 1\n");
9901 }
9902
9903 return 0;
9904}
9905
9906static int lcdshadow_write(char *buf)
9907{
9908 char *cmd;
Andy Shevchenko466f4692020-05-11 15:54:14 +03009909 int res, state = -EINVAL;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009910
Hans de Goedef8098912021-10-05 22:23:20 +02009911 if (!lcdshadow_dev)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009912 return -ENODEV;
9913
Andy Shevchenkobe51bd42020-05-11 15:56:24 +03009914 while ((cmd = strsep(&buf, ","))) {
Andy Shevchenko466f4692020-05-11 15:54:14 +03009915 res = kstrtoint(cmd, 10, &state);
9916 if (res < 0)
9917 return res;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009918 }
9919
Andy Shevchenko466f4692020-05-11 15:54:14 +03009920 if (state >= 2 || state < 0)
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009921 return -EINVAL;
9922
Hans de Goedef8098912021-10-05 22:23:20 +02009923 mutex_lock(&lcdshadow_dev->lock);
9924 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
9925 mutex_unlock(&lcdshadow_dev->lock);
9926
9927 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
9928
9929 return res;
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009930}
9931
9932static struct ibm_struct lcdshadow_driver_data = {
9933 .name = "lcdshadow",
Hans de Goedef8098912021-10-05 22:23:20 +02009934 .exit = lcdshadow_exit,
Alexander Schremmer110ea1d2019-08-22 13:48:33 +02009935 .resume = lcdshadow_resume,
9936 .read = lcdshadow_read,
9937 .write = lcdshadow_write,
9938};
9939
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009940/*************************************************************************
Mark Pearson1ac09652020-11-24 13:11:54 -05009941 * Thinkpad sensor interfaces
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009942 */
9943
Mark Pearson0b4f5c92021-03-11 12:48:43 -05009944#define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
9945#define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
9946#define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
9947#define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
9948
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009949#define DYTC_CMD_GET 2 /* To get current IC function and mode */
9950#define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
9951
Mark Pearson1ac09652020-11-24 13:11:54 -05009952#define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
9953#define PALMSENSOR_ON_BIT 1 /* psensor status */
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009954
Mark Pearson1ac09652020-11-24 13:11:54 -05009955static bool has_palmsensor;
9956static bool has_lapsensor;
9957static bool palm_state;
9958static bool lap_state;
Mark Pearson0b4f5c92021-03-11 12:48:43 -05009959static int dytc_version;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009960
Mark Pearsonc3bfcd42021-01-11 11:22:37 -05009961static int dytc_command(int command, int *output)
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009962{
9963 acpi_handle dytc_handle;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -05009964
9965 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
9966 /* Platform doesn't support DYTC */
9967 return -ENODEV;
9968 }
9969 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
9970 return -EIO;
9971 return 0;
9972}
9973
9974static int lapsensor_get(bool *present, bool *state)
9975{
9976 int output, err;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009977
Mark Pearson1ac09652020-11-24 13:11:54 -05009978 *present = false;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -05009979 err = dytc_command(DYTC_CMD_GET, &output);
9980 if (err)
9981 return err;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009982
Mark Pearson1ac09652020-11-24 13:11:54 -05009983 *present = true; /*If we get his far, we have lapmode support*/
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009984 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
9985 return 0;
9986}
9987
Mark Pearson1ac09652020-11-24 13:11:54 -05009988static int palmsensor_get(bool *present, bool *state)
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009989{
Mark Pearson1ac09652020-11-24 13:11:54 -05009990 acpi_handle psensor_handle;
9991 int output;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009992
Mark Pearson1ac09652020-11-24 13:11:54 -05009993 *present = false;
9994 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
9995 return -ENODEV;
9996 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
9997 return -EIO;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -04009998
Mark Pearson1ac09652020-11-24 13:11:54 -05009999 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
10000 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10001 return 0;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010002}
10003
Mark Pearson1ac09652020-11-24 13:11:54 -050010004static void lapsensor_refresh(void)
10005{
10006 bool state;
10007 int err;
10008
10009 if (has_lapsensor) {
10010 err = lapsensor_get(&has_lapsensor, &state);
10011 if (err)
10012 return;
10013 if (lap_state != state) {
10014 lap_state = state;
10015 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10016 }
10017 }
10018}
10019
10020static void palmsensor_refresh(void)
10021{
10022 bool state;
10023 int err;
10024
10025 if (has_palmsensor) {
10026 err = palmsensor_get(&has_palmsensor, &state);
10027 if (err)
10028 return;
10029 if (palm_state != state) {
10030 palm_state = state;
10031 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10032 }
10033 }
10034}
10035
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010036static ssize_t dytc_lapmode_show(struct device *dev,
10037 struct device_attribute *attr,
10038 char *buf)
10039{
Mark Pearson1ac09652020-11-24 13:11:54 -050010040 if (has_lapsensor)
10041 return sysfs_emit(buf, "%d\n", lap_state);
10042 return sysfs_emit(buf, "\n");
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010043}
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010044static DEVICE_ATTR_RO(dytc_lapmode);
10045
Mark Pearson1ac09652020-11-24 13:11:54 -050010046static ssize_t palmsensor_show(struct device *dev,
10047 struct device_attribute *attr,
10048 char *buf)
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010049{
Mark Pearson1ac09652020-11-24 13:11:54 -050010050 if (has_palmsensor)
10051 return sysfs_emit(buf, "%d\n", palm_state);
10052 return sysfs_emit(buf, "\n");
10053}
10054static DEVICE_ATTR_RO(palmsensor);
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010055
Len Baker79f960e2021-10-23 17:40:36 +020010056static struct attribute *proxsensor_attributes[] = {
10057 &dev_attr_dytc_lapmode.attr,
10058 &dev_attr_palmsensor.attr,
10059 NULL
10060};
10061
10062static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
10063 struct attribute *attr, int n)
10064{
10065 if (attr == &dev_attr_dytc_lapmode.attr) {
10066 /*
10067 * Platforms before DYTC version 5 claim to have a lap sensor,
10068 * but it doesn't work, so we ignore them.
10069 */
Hans de Goede5a47ac02021-11-21 20:11:25 +010010070 if (!has_lapsensor || dytc_version < 5)
Len Baker79f960e2021-10-23 17:40:36 +020010071 return 0;
10072 } else if (attr == &dev_attr_palmsensor.attr) {
10073 if (!has_palmsensor)
10074 return 0;
10075 }
10076
10077 return attr->mode;
10078}
10079
10080static const struct attribute_group proxsensor_attr_group = {
10081 .is_visible = proxsensor_attr_is_visible,
10082 .attrs = proxsensor_attributes,
10083};
10084
Mark Pearson1ac09652020-11-24 13:11:54 -050010085static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10086{
Hans de Goede5a47ac02021-11-21 20:11:25 +010010087 int palm_err, lap_err;
Mark Pearson1ac09652020-11-24 13:11:54 -050010088
10089 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10090 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
Hans de Goedec7e1c782021-11-21 20:11:24 +010010091 /* If support isn't available for both devices return -ENODEV */
Mark Pearson1ac09652020-11-24 13:11:54 -050010092 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
Hans de Goedec7e1c782021-11-21 20:11:24 +010010093 return -ENODEV;
Mark Pearson1ac09652020-11-24 13:11:54 -050010094 /* Otherwise, if there was an error return it */
Mark Pearsonaa44afa2020-12-29 21:47:26 -050010095 if (palm_err && (palm_err != -ENODEV))
Mark Pearson1ac09652020-11-24 13:11:54 -050010096 return palm_err;
Mark Pearsonaa44afa2020-12-29 21:47:26 -050010097 if (lap_err && (lap_err != -ENODEV))
Mark Pearson1ac09652020-11-24 13:11:54 -050010098 return lap_err;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010099
Mark Pearson1ac09652020-11-24 13:11:54 -050010100 return 0;
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010101}
10102
Mark Pearson1ac09652020-11-24 13:11:54 -050010103static struct ibm_struct proxsensor_driver_data = {
10104 .name = "proximity-sensor",
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010105};
10106
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010107/*************************************************************************
10108 * DYTC Platform Profile interface
10109 */
10110
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010111#define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
Mark Pearson7a47f862021-04-06 19:32:03 -040010112#define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010113#define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10114
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010115#define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10116#define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10117
10118#define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10119#define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10120#define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10121
10122#define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10123#define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10124#define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
10125
10126#define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
10127#define DYTC_MODE_LOWPOWER 3 /* Low power mode */
10128#define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
Mark Pearson7a47f862021-04-06 19:32:03 -040010129#define DYTC_MODE_MMC_BALANCE 0 /* Default mode from MMC_GET, aka balanced */
10130
10131#define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10132#define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010133
10134#define DYTC_SET_COMMAND(function, mode, on) \
10135 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10136 (mode) << DYTC_SET_MODE_BIT | \
10137 (on) << DYTC_SET_VALID_BIT)
10138
10139#define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
10140
10141#define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
10142
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010143static enum platform_profile_option dytc_current_profile;
10144static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10145static DEFINE_MUTEX(dytc_mutex);
Mark Pearson7a47f862021-04-06 19:32:03 -040010146static bool dytc_mmc_get_available;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010147
10148static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
10149{
10150 switch (dytcmode) {
10151 case DYTC_MODE_LOWPOWER:
10152 *profile = PLATFORM_PROFILE_LOW_POWER;
10153 break;
10154 case DYTC_MODE_BALANCE:
Mark Pearson7a47f862021-04-06 19:32:03 -040010155 case DYTC_MODE_MMC_BALANCE:
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010156 *profile = PLATFORM_PROFILE_BALANCED;
10157 break;
10158 case DYTC_MODE_PERFORM:
10159 *profile = PLATFORM_PROFILE_PERFORMANCE;
10160 break;
10161 default: /* Unknown mode */
10162 return -EINVAL;
10163 }
10164 return 0;
10165}
10166
10167static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10168{
10169 switch (profile) {
10170 case PLATFORM_PROFILE_LOW_POWER:
10171 *perfmode = DYTC_MODE_LOWPOWER;
10172 break;
10173 case PLATFORM_PROFILE_BALANCED:
10174 *perfmode = DYTC_MODE_BALANCE;
10175 break;
10176 case PLATFORM_PROFILE_PERFORMANCE:
10177 *perfmode = DYTC_MODE_PERFORM;
10178 break;
10179 default: /* Unknown profile */
10180 return -EOPNOTSUPP;
10181 }
10182 return 0;
10183}
10184
10185/*
10186 * dytc_profile_get: Function to register with platform_profile
10187 * handler. Returns current platform profile.
10188 */
Hans de Goeded2386d72021-02-04 15:01:57 +010010189static int dytc_profile_get(struct platform_profile_handler *pprof,
10190 enum platform_profile_option *profile)
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010191{
10192 *profile = dytc_current_profile;
10193 return 0;
10194}
10195
10196/*
10197 * Helper function - check if we are in CQL mode and if we are
10198 * - disable CQL,
10199 * - run the command
10200 * - enable CQL
10201 * If not in CQL mode, just run the command
10202 */
Hans de Goeded2386d72021-02-04 15:01:57 +010010203static int dytc_cql_command(int command, int *output)
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010204{
10205 int err, cmd_err, dummy;
10206 int cur_funcmode;
10207
10208 /* Determine if we are in CQL mode. This alters the commands we do */
10209 err = dytc_command(DYTC_CMD_GET, output);
10210 if (err)
10211 return err;
10212
10213 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10214 /* Check if we're OK to return immediately */
10215 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10216 return 0;
10217
10218 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10219 atomic_inc(&dytc_ignore_event);
10220 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10221 if (err)
10222 return err;
10223 }
10224
10225 cmd_err = dytc_command(command, output);
10226 /* Check return condition after we've restored CQL state */
10227
10228 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10229 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10230 if (err)
10231 return err;
10232 }
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010233 return cmd_err;
10234}
10235
10236/*
10237 * dytc_profile_set: Function to register with platform_profile
10238 * handler. Sets current platform profile.
10239 */
Hans de Goeded2386d72021-02-04 15:01:57 +010010240static int dytc_profile_set(struct platform_profile_handler *pprof,
10241 enum platform_profile_option profile)
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010242{
10243 int output;
10244 int err;
10245
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010246 err = mutex_lock_interruptible(&dytc_mutex);
10247 if (err)
10248 return err;
10249
10250 if (profile == PLATFORM_PROFILE_BALANCED) {
Hans de Goedee157c952021-03-21 12:31:08 +010010251 /*
10252 * To get back to balanced mode we need to issue a reset command.
10253 * Note we still need to disable CQL mode before hand and re-enable
10254 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10255 * stuck at 0 for aprox. 30 minutes.
10256 */
10257 err = dytc_cql_command(DYTC_CMD_RESET, &output);
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010258 if (err)
10259 goto unlock;
10260 } else {
10261 int perfmode;
10262
10263 err = convert_profile_to_dytc(profile, &perfmode);
10264 if (err)
10265 goto unlock;
10266
10267 /* Determine if we are in CQL mode. This alters the commands we do */
10268 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1), &output);
10269 if (err)
10270 goto unlock;
10271 }
10272 /* Success - update current profile */
10273 dytc_current_profile = profile;
10274unlock:
10275 mutex_unlock(&dytc_mutex);
10276 return err;
10277}
10278
10279static void dytc_profile_refresh(void)
10280{
10281 enum platform_profile_option profile;
10282 int output, err;
10283 int perfmode;
10284
10285 mutex_lock(&dytc_mutex);
Mark Pearson7a47f862021-04-06 19:32:03 -040010286 if (dytc_mmc_get_available)
10287 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10288 else
10289 err = dytc_cql_command(DYTC_CMD_GET, &output);
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010290 mutex_unlock(&dytc_mutex);
10291 if (err)
10292 return;
10293
10294 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10295 convert_dytc_to_profile(perfmode, &profile);
10296 if (profile != dytc_current_profile) {
10297 dytc_current_profile = profile;
10298 platform_profile_notify();
10299 }
10300}
10301
10302static struct platform_profile_handler dytc_profile = {
10303 .profile_get = dytc_profile_get,
10304 .profile_set = dytc_profile_set,
10305};
10306
10307static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10308{
10309 int err, output;
10310
10311 /* Setup supported modes */
10312 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10313 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10314 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10315
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010316 err = dytc_command(DYTC_CMD_QUERY, &output);
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010317 if (err)
10318 return err;
10319
Hans de Goede5a47ac02021-11-21 20:11:25 +010010320 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
10321 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010322
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010323 /* Check DYTC is enabled and supports mode setting */
Hans de Goede0b0d2fb2021-11-21 20:11:26 +010010324 if (dytc_version < 5)
10325 return -ENODEV;
10326
Hans de Goede798682e2021-11-21 20:11:27 +010010327 dbg_printk(TPACPI_DBG_INIT,
10328 "DYTC version %d: thermal mode available\n", dytc_version);
10329 /*
10330 * Check if MMC_GET functionality available
10331 * Version > 6 and return success from MMC_GET command
10332 */
10333 dytc_mmc_get_available = false;
10334 if (dytc_version >= 6) {
10335 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10336 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10337 dytc_mmc_get_available = true;
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010338 }
Hans de Goede798682e2021-11-21 20:11:27 +010010339 /* Create platform_profile structure and register */
10340 err = platform_profile_register(&dytc_profile);
10341 /*
10342 * If for some reason platform_profiles aren't enabled
10343 * don't quit terminally.
10344 */
10345 if (err)
10346 return -ENODEV;
10347
10348 /* Ensure initial values are correct */
10349 dytc_profile_refresh();
10350
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010351 return 0;
10352}
10353
10354static void dytc_profile_exit(void)
10355{
Hans de Goede0b0d2fb2021-11-21 20:11:26 +010010356 platform_profile_remove();
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010357}
10358
10359static struct ibm_struct dytc_profile_driver_data = {
10360 .name = "dytc-profile",
10361 .exit = dytc_profile_exit,
10362};
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010363
Nitin Joshid7cbe272021-01-25 11:59:16 +090010364/*************************************************************************
10365 * Keyboard language interface
10366 */
10367
10368struct keyboard_lang_data {
10369 const char *lang_str;
10370 int lang_code;
10371};
10372
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010373static const struct keyboard_lang_data keyboard_lang_data[] = {
Nitin Joshid7cbe272021-01-25 11:59:16 +090010374 {"be", 0x080c},
10375 {"cz", 0x0405},
10376 {"da", 0x0406},
10377 {"de", 0x0c07},
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010378 {"en", 0x0000},
Nitin Joshid7cbe272021-01-25 11:59:16 +090010379 {"es", 0x2c0a},
10380 {"et", 0x0425},
10381 {"fr", 0x040c},
10382 {"fr-ch", 0x100c},
10383 {"hu", 0x040e},
10384 {"it", 0x0410},
10385 {"jp", 0x0411},
10386 {"nl", 0x0413},
10387 {"nn", 0x0414},
10388 {"pl", 0x0415},
10389 {"pt", 0x0816},
10390 {"sl", 0x041b},
10391 {"sv", 0x081d},
10392 {"tr", 0x041f},
10393};
10394
10395static int set_keyboard_lang_command(int command)
10396{
10397 acpi_handle sskl_handle;
10398 int output;
10399
10400 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10401 /* Platform doesn't support SSKL */
10402 return -ENODEV;
10403 }
10404
10405 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10406 return -EIO;
10407
10408 return 0;
10409}
10410
10411static int get_keyboard_lang(int *output)
10412{
10413 acpi_handle gskl_handle;
10414 int kbd_lang;
10415
10416 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10417 /* Platform doesn't support GSKL */
10418 return -ENODEV;
10419 }
10420
10421 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10422 return -EIO;
10423
Hans de Goede9e9c6412021-01-25 21:52:58 +010010424 /*
10425 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10426 * '(' and ')') keys which use layout dependent key-press emulation.
10427 */
10428 if (kbd_lang & METHOD_ERR)
10429 return -ENODEV;
10430
Nitin Joshid7cbe272021-01-25 11:59:16 +090010431 *output = kbd_lang;
10432
10433 return 0;
10434}
10435
10436/* sysfs keyboard language entry */
10437static ssize_t keyboard_lang_show(struct device *dev,
10438 struct device_attribute *attr,
10439 char *buf)
10440{
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010441 int output, err, i, len = 0;
Nitin Joshid7cbe272021-01-25 11:59:16 +090010442
10443 err = get_keyboard_lang(&output);
10444 if (err)
10445 return err;
10446
10447 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10448 if (i)
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010449 len += sysfs_emit_at(buf, len, "%s", " ");
Nitin Joshid7cbe272021-01-25 11:59:16 +090010450
10451 if (output == keyboard_lang_data[i].lang_code) {
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010452 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
Nitin Joshid7cbe272021-01-25 11:59:16 +090010453 } else {
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010454 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
Nitin Joshid7cbe272021-01-25 11:59:16 +090010455 }
10456 }
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010457 len += sysfs_emit_at(buf, len, "\n");
Nitin Joshid7cbe272021-01-25 11:59:16 +090010458
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010459 return len;
Nitin Joshid7cbe272021-01-25 11:59:16 +090010460}
10461
10462static ssize_t keyboard_lang_store(struct device *dev,
10463 struct device_attribute *attr,
10464 const char *buf, size_t count)
10465{
10466 int err, i;
10467 bool lang_found = false;
10468 int lang_code = 0;
10469
10470 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10471 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10472 lang_code = keyboard_lang_data[i].lang_code;
10473 lang_found = true;
10474 break;
10475 }
10476 }
10477
10478 if (lang_found) {
10479 lang_code = lang_code | 1 << 24;
10480
10481 /* Set language code */
10482 err = set_keyboard_lang_command(lang_code);
10483 if (err)
10484 return err;
10485 } else {
Nitin Joshicfa75cc2021-02-02 09:32:10 +090010486 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
Nitin Joshid7cbe272021-01-25 11:59:16 +090010487 return -EINVAL;
10488 }
10489
10490 tpacpi_disclose_usertask(attr->attr.name,
10491 "keyboard language is set to %s\n", buf);
10492
10493 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10494
10495 return count;
10496}
Nitin Joshid7cbe272021-01-25 11:59:16 +090010497static DEVICE_ATTR_RW(keyboard_lang);
10498
10499static struct attribute *kbdlang_attributes[] = {
10500 &dev_attr_keyboard_lang.attr,
10501 NULL
10502};
10503
Len Baker79f960e2021-10-23 17:40:36 +020010504static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
10505 struct attribute *attr, int n)
10506{
10507 return tp_features.kbd_lang ? attr->mode : 0;
10508}
10509
Nitin Joshid7cbe272021-01-25 11:59:16 +090010510static const struct attribute_group kbdlang_attr_group = {
Len Baker79f960e2021-10-23 17:40:36 +020010511 .is_visible = kbdlang_attr_is_visible,
Nitin Joshid7cbe272021-01-25 11:59:16 +090010512 .attrs = kbdlang_attributes,
10513};
10514
10515static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10516{
10517 int err, output;
10518
10519 err = get_keyboard_lang(&output);
Len Baker79f960e2021-10-23 17:40:36 +020010520 tp_features.kbd_lang = !err;
10521 return err;
Nitin Joshid7cbe272021-01-25 11:59:16 +090010522}
10523
10524static struct ibm_struct kbdlang_driver_data = {
10525 .name = "kbdlang",
Nitin Joshid7cbe272021-01-25 11:59:16 +090010526};
10527
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010528/*************************************************************************
10529 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10530 * and WLAN feature.
10531 */
10532#define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10533#define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10534#define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10535static bool has_antennatype;
10536static int wwan_antennatype;
10537
10538static int dprc_command(int command, int *output)
10539{
10540 acpi_handle dprc_handle;
10541
10542 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10543 /* Platform doesn't support DPRC */
10544 return -ENODEV;
10545 }
10546
10547 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10548 return -EIO;
10549
10550 /*
10551 * METHOD_ERR gets returned on devices where few commands are not supported
10552 * for example command to get WWAN Antenna type command is not supported on
10553 * some devices.
10554 */
10555 if (*output & METHOD_ERR)
10556 return -ENODEV;
10557
10558 return 0;
10559}
10560
10561static int get_wwan_antenna(int *wwan_antennatype)
10562{
10563 int output, err;
10564
10565 /* Get current Antenna type */
10566 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10567 if (err)
10568 return err;
10569
10570 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10571 *wwan_antennatype = 1;
10572 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10573 *wwan_antennatype = 2;
10574 else
10575 return -ENODEV;
10576
10577 return 0;
10578}
10579
10580/* sysfs wwan antenna type entry */
10581static ssize_t wwan_antenna_type_show(struct device *dev,
10582 struct device_attribute *attr,
10583 char *buf)
10584{
10585 switch (wwan_antennatype) {
10586 case 1:
10587 return sysfs_emit(buf, "type a\n");
10588 case 2:
10589 return sysfs_emit(buf, "type b\n");
10590 default:
10591 return -ENODATA;
10592 }
10593}
10594static DEVICE_ATTR_RO(wwan_antenna_type);
10595
Len Baker79f960e2021-10-23 17:40:36 +020010596static struct attribute *dprc_attributes[] = {
10597 &dev_attr_wwan_antenna_type.attr,
10598 NULL
10599};
10600
10601static umode_t dprc_attr_is_visible(struct kobject *kobj,
10602 struct attribute *attr, int n)
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010603{
Len Baker79f960e2021-10-23 17:40:36 +020010604 return has_antennatype ? attr->mode : 0;
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010605}
10606
Len Baker79f960e2021-10-23 17:40:36 +020010607static const struct attribute_group dprc_attr_group = {
10608 .is_visible = dprc_attr_is_visible,
10609 .attrs = dprc_attributes,
10610};
10611
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010612static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010613{
Hans de Goedec7e1c782021-11-21 20:11:24 +010010614 int err;
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010615
Hans de Goedec7e1c782021-11-21 20:11:24 +010010616 err = get_wwan_antenna(&wwan_antennatype);
Len Baker79f960e2021-10-23 17:40:36 +020010617 if (err)
10618 return err;
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010619
Len Baker79f960e2021-10-23 17:40:36 +020010620 has_antennatype = true;
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010621 return 0;
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010622}
10623
10624static struct ibm_struct dprc_driver_data = {
10625 .name = "dprc",
Len Baker79f960e2021-10-23 17:40:36 +020010626};
10627
10628/* --------------------------------------------------------------------- */
10629
Hans de Goede5cd68962021-11-23 22:04:21 +010010630static struct attribute *tpacpi_driver_attributes[] = {
Len Baker79f960e2021-10-23 17:40:36 +020010631 &driver_attr_debug_level.attr,
10632 &driver_attr_version.attr,
10633 &driver_attr_interface_version.attr,
10634#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10635 &driver_attr_wlsw_emulstate.attr,
10636 &driver_attr_bluetooth_emulstate.attr,
10637 &driver_attr_wwan_emulstate.attr,
10638 &driver_attr_uwb_emulstate.attr,
10639#endif
10640 NULL
10641};
10642
10643#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10644static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
10645 struct attribute *attr, int n)
10646{
10647 if (attr == &driver_attr_wlsw_emulstate.attr) {
10648 if (!dbg_wlswemul)
10649 return 0;
10650 } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
10651 if (!dbg_bluetoothemul)
10652 return 0;
10653 } else if (attr == &driver_attr_wwan_emulstate.attr) {
10654 if (!dbg_wwanemul)
10655 return 0;
10656 } else if (attr == &driver_attr_uwb_emulstate.attr) {
10657 if (!dbg_uwbemul)
10658 return 0;
10659 }
10660
10661 return attr->mode;
10662}
10663#endif
10664
Hans de Goede5cd68962021-11-23 22:04:21 +010010665static const struct attribute_group tpacpi_driver_attr_group = {
Len Baker79f960e2021-10-23 17:40:36 +020010666#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10667 .is_visible = tpacpi_attr_is_visible,
10668#endif
Hans de Goede5cd68962021-11-23 22:04:21 +010010669 .attrs = tpacpi_driver_attributes,
10670};
10671
10672static const struct attribute_group *tpacpi_driver_groups[] = {
10673 &tpacpi_driver_attr_group,
10674 NULL,
Len Baker79f960e2021-10-23 17:40:36 +020010675};
10676
10677static const struct attribute_group *tpacpi_groups[] = {
10678 &adaptive_kbd_attr_group,
10679 &hotkey_attr_group,
10680 &bluetooth_attr_group,
10681 &wan_attr_group,
10682 &cmos_attr_group,
10683 &proxsensor_attr_group,
10684 &kbdlang_attr_group,
10685 &dprc_attr_group,
Len Baker79f960e2021-10-23 17:40:36 +020010686 NULL,
10687};
10688
10689static const struct attribute_group *tpacpi_hwmon_groups[] = {
10690 &thermal_attr_group,
10691 &temp_label_attr_group,
10692 &fan_attr_group,
10693 NULL,
10694};
10695
Hans de Goede5cd68962021-11-23 22:04:21 +010010696static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
10697 &fan_driver_attr_group,
10698 NULL,
10699};
10700
Len Baker79f960e2021-10-23 17:40:36 +020010701/****************************************************************************
10702 ****************************************************************************
10703 *
10704 * Platform drivers
10705 *
10706 ****************************************************************************
10707 ****************************************************************************/
10708
10709static struct platform_driver tpacpi_pdriver = {
10710 .driver = {
10711 .name = TPACPI_DRVR_NAME,
10712 .pm = &tpacpi_pm,
Hans de Goede5cd68962021-11-23 22:04:21 +010010713 .groups = tpacpi_driver_groups,
Len Baker79f960e2021-10-23 17:40:36 +020010714 .dev_groups = tpacpi_groups,
10715 },
10716 .shutdown = tpacpi_shutdown_handler,
10717};
10718
10719static struct platform_driver tpacpi_hwmon_pdriver = {
10720 .driver = {
10721 .name = TPACPI_HWMON_DRVR_NAME,
Hans de Goede5cd68962021-11-23 22:04:21 +010010722 .groups = tpacpi_hwmon_driver_groups,
Len Baker79f960e2021-10-23 17:40:36 +020010723 },
Nitin Joshi3feb52a2021-03-17 11:46:36 +090010724};
10725
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010726/****************************************************************************
10727 ****************************************************************************
10728 *
10729 * Infrastructure
10730 *
10731 ****************************************************************************
10732 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070010733
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010734/*
10735 * HKEY event callout for other subdrivers go here
10736 * (yes, it is ugly, but it is quick, safe, and gets the job done
10737 */
10738static void tpacpi_driver_event(const unsigned int hkey_event)
10739{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +000010740 if (ibm_backlight_device) {
10741 switch (hkey_event) {
10742 case TP_HKEY_EV_BRGHT_UP:
10743 case TP_HKEY_EV_BRGHT_DOWN:
10744 tpacpi_brightness_notify_change();
10745 }
10746 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010747 if (alsa_card) {
10748 switch (hkey_event) {
10749 case TP_HKEY_EV_VOL_UP:
10750 case TP_HKEY_EV_VOL_DOWN:
10751 case TP_HKEY_EV_VOL_MUTE:
10752 volume_alsa_notify_change();
10753 }
10754 }
Hans de Goedec685e202017-02-09 16:44:13 +010010755 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
10756 enum led_brightness brightness;
10757
10758 mutex_lock(&kbdlight_mutex);
10759
10760 /*
10761 * Check the brightness actually changed, setting the brightness
10762 * through kbdlight_set_level() also triggers this event.
10763 */
10764 brightness = kbdlight_sysfs_get(NULL);
10765 if (kbdlight_brightness != brightness) {
10766 kbdlight_brightness = brightness;
10767 led_classdev_notify_brightness_hw_changed(
10768 &tpacpi_led_kbdlight.led_classdev, brightness);
10769 }
10770
10771 mutex_unlock(&kbdlight_mutex);
10772 }
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040010773
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010774 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
Mark Pearson1ac09652020-11-24 13:11:54 -050010775 lapsensor_refresh();
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010776 /* If we are already accessing DYTC then skip dytc update */
10777 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
10778 dytc_profile_refresh();
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050010779 }
Hans de Goedef8098912021-10-05 22:23:20 +020010780
10781 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
10782 enum drm_privacy_screen_status old_hw_state;
10783 bool changed;
10784
10785 mutex_lock(&lcdshadow_dev->lock);
10786 old_hw_state = lcdshadow_dev->hw_state;
10787 lcdshadow_get_hw_state(lcdshadow_dev);
10788 changed = lcdshadow_dev->hw_state != old_hw_state;
10789 mutex_unlock(&lcdshadow_dev->lock);
10790
10791 if (changed)
10792 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10793 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010794}
10795
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010796static void hotkey_driver_event(const unsigned int scancode)
10797{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -030010798 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -030010799}
10800
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010801/* --------------------------------------------------------------------- */
10802
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010803/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -030010804static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010805
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010806/*
10807 * Module and infrastructure proble, init and exit handling
10808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010809
Rusty Russell90ab5ee2012-01-13 09:32:20 +103010810static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010811
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010812#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010813static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010814{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010815 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010816
Jan van den Berg72a979f2014-09-17 00:01:08 +020010817 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010818}
10819#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
10820
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010821static void ibm_exit(struct ibm_struct *ibm)
10822{
10823 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
10824
10825 list_del_init(&ibm->all_drivers);
10826
10827 if (ibm->flags.acpi_notify_installed) {
10828 dbg_printk(TPACPI_DBG_EXIT,
10829 "%s: acpi_remove_notify_handler\n", ibm->name);
10830 BUG_ON(!ibm->acpi);
10831 acpi_remove_notify_handler(*ibm->acpi->handle,
10832 ibm->acpi->type,
10833 dispatch_acpi_notify);
10834 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010835 }
10836
10837 if (ibm->flags.proc_created) {
10838 dbg_printk(TPACPI_DBG_EXIT,
10839 "%s: remove_proc_entry\n", ibm->name);
10840 remove_proc_entry(ibm->name, proc_dir);
10841 ibm->flags.proc_created = 0;
10842 }
10843
10844 if (ibm->flags.acpi_driver_registered) {
10845 dbg_printk(TPACPI_DBG_EXIT,
10846 "%s: acpi_bus_unregister_driver\n", ibm->name);
10847 BUG_ON(!ibm->acpi);
10848 acpi_bus_unregister_driver(ibm->acpi->driver);
10849 kfree(ibm->acpi->driver);
10850 ibm->acpi->driver = NULL;
10851 ibm->flags.acpi_driver_registered = 0;
10852 }
10853
10854 if (ibm->flags.init_called && ibm->exit) {
10855 ibm->exit();
10856 ibm->flags.init_called = 0;
10857 }
10858
10859 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
10860}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020010861
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010862static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010863{
10864 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010865 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010866 struct proc_dir_entry *entry;
10867
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010868 BUG_ON(ibm == NULL);
10869
10870 INIT_LIST_HEAD(&ibm->all_drivers);
10871
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -030010872 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010873 return 0;
10874
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010875 dbg_printk(TPACPI_DBG_INIT,
10876 "probing for %s\n", ibm->name);
10877
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010878 if (iibm->init) {
10879 ret = iibm->init(iibm);
Hans de Goedec15f8682021-11-21 20:11:23 +010010880 if (ret > 0 || ret == -ENODEV)
10881 return 0; /* subdriver functionality not available */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010882 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010883 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010884
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -030010885 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010886 }
10887
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -030010888 if (ibm->acpi) {
10889 if (ibm->acpi->hid) {
10890 ret = register_tpacpi_subdriver(ibm);
10891 if (ret)
10892 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010893 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -030010894
10895 if (ibm->acpi->notify) {
10896 ret = setup_acpi_notify(ibm);
10897 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -070010898 pr_notice("disabling subdriver %s\n",
10899 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -030010900 ret = 0;
10901 goto err_out;
10902 }
10903 if (ret < 0)
10904 goto err_out;
10905 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010906 }
10907
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010908 dbg_printk(TPACPI_DBG_INIT,
10909 "%s installed\n", ibm->name);
10910
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010911 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -040010912 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -020010913
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -030010914 if (!mode)
10915 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -020010916 if (ibm->write)
10917 mode |= S_IWUSR;
10918 entry = proc_create_data(ibm->name, mode, proc_dir,
Alexey Dobriyan97a32532020-02-03 17:37:17 -080010919 &dispatch_proc_ops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010920 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -070010921 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010922 ret = -ENODEV;
10923 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010924 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -030010925 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010928 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
10929
Linus Torvalds1da177e2005-04-16 15:20:36 -070010930 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010931
10932err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -030010933 dbg_printk(TPACPI_DBG_INIT,
10934 "%s: at error exit path with result %d\n",
10935 ibm->name, ret);
10936
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010937 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +020010938 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010939}
10940
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010941/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010942
Jouke Witteveen846a4162018-07-11 11:44:41 +020010943static char __init tpacpi_parse_fw_id(const char * const s,
10944 u32 *model, u16 *release)
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010945{
Jouke Witteveen846a4162018-07-11 11:44:41 +020010946 int i;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010947
Jouke Witteveen846a4162018-07-11 11:44:41 +020010948 if (!s || strlen(s) < 8)
10949 goto invalid;
10950
10951 for (i = 0; i < 8; i++)
10952 if (!((s[i] >= '0' && s[i] <= '9') ||
10953 (s[i] >= 'A' && s[i] <= 'Z')))
10954 goto invalid;
10955
Adam Lee1b0eb5b2015-02-11 13:43:10 +080010956 /*
10957 * Most models: xxyTkkWW (#.##c)
10958 * Ancient 570/600 and -SL lacks (#.##c)
10959 */
Jouke Witteveen846a4162018-07-11 11:44:41 +020010960 if (s[3] == 'T' || s[3] == 'N') {
10961 *model = TPID(s[0], s[1]);
10962 *release = TPVER(s[4], s[5]);
10963 return s[2];
Adam Lee1b0eb5b2015-02-11 13:43:10 +080010964
10965 /* New models: xxxyTkkW (#.##c); T550 and some others */
Jouke Witteveen846a4162018-07-11 11:44:41 +020010966 } else if (s[4] == 'T' || s[4] == 'N') {
10967 *model = TPID3(s[0], s[1], s[2]);
10968 *release = TPVER(s[5], s[6]);
10969 return s[3];
10970 }
10971
10972invalid:
10973 return '\0';
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010974}
10975
Jiaxun Yang9a4b33d2019-03-08 21:14:26 +080010976static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10977{
10978 char *ec_fw_string = (char *) private;
10979 const char *dmi_data = (const char *)dm;
10980 /*
10981 * ThinkPad Embedded Controller Program Table on newer models
10982 *
10983 * Offset | Name | Width | Description
10984 * ----------------------------------------------------
10985 * 0x00 | Type | BYTE | 0x8C
10986 * 0x01 | Length | BYTE |
10987 * 0x02 | Handle | WORD | Varies
10988 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
10989 * 0x0A | OEM struct offset | BYTE | 0x0B
10990 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
10991 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
10992 * 0x0D | ECP version ID | STR ID |
10993 * 0x0E | ECP release date | STR ID |
10994 */
10995
10996 /* Return if data structure not match */
10997 if (dm->type != 140 || dm->length < 0x0F ||
10998 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
10999 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
11000 dmi_data[0x0C] != 0x01)
11001 return;
11002
11003 /* fwstr is the first 8byte string */
11004 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
11005}
11006
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011007/* returns 0 - probe ok, or < 0 - probe error.
11008 * Probe ok doesn't mean thinkpad found.
11009 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
11010static int __must_check __init get_thinkpad_model_data(
11011 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020011012{
Jeff Garzik18552562007-10-03 15:15:40 -040011013 const struct dmi_device *dev = NULL;
Jiaxun Yang9a4b33d2019-03-08 21:14:26 +080011014 char ec_fw_string[18] = {0};
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011015 char const *s;
Jouke Witteveen846a4162018-07-11 11:44:41 +020011016 char t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020011017
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011018 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011019 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011020
11021 memset(tp, 0, sizeof(*tp));
11022
11023 if (dmi_name_in_vendors("IBM"))
11024 tp->vendor = PCI_VENDOR_ID_IBM;
11025 else if (dmi_name_in_vendors("LENOVO"))
11026 tp->vendor = PCI_VENDOR_ID_LENOVO;
11027 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011028 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011029
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011030 s = dmi_get_system_info(DMI_BIOS_VERSION);
11031 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
11032 if (s && !tp->bios_version_str)
11033 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030011034
11035 /* Really ancient ThinkPad 240X will fail this, which is fine */
Jouke Witteveen846a4162018-07-11 11:44:41 +020011036 t = tpacpi_parse_fw_id(tp->bios_version_str,
11037 &tp->bios_model, &tp->bios_release);
11038 if (t != 'E' && t != 'C')
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011039 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030011040
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020011041 /*
11042 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
11043 * X32 or newer, all Z series; Some models must have an
11044 * up-to-date BIOS or they will not be detected.
11045 *
Alexander A. Klimov19206a12020-07-13 21:06:28 +020011046 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020011047 */
11048 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020011049 if (sscanf(dev->name,
11050 "IBM ThinkPad Embedded Controller -[%17c",
11051 ec_fw_string) == 1) {
11052 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
11053 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011054 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020011055 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020011056 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011057
Jiaxun Yang9a4b33d2019-03-08 21:14:26 +080011058 /* Newer ThinkPads have different EC program info table */
11059 if (!ec_fw_string[0])
11060 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
11061
11062 if (ec_fw_string[0]) {
11063 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
11064 if (!tp->ec_version_str)
11065 return -ENOMEM;
11066
11067 t = tpacpi_parse_fw_id(ec_fw_string,
11068 &tp->ec_model, &tp->ec_release);
11069 if (t != 'H') {
11070 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
11071 ec_fw_string);
11072 pr_notice("please report this to %s\n", TPACPI_MAIL);
11073 }
11074 }
11075
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011076 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -070011077 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011078 tp->model_str = kstrdup(s, GFP_KERNEL);
11079 if (!tp->model_str)
11080 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -050011081 } else {
11082 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -070011083 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -050011084 tp->model_str = kstrdup(s, GFP_KERNEL);
11085 if (!tp->model_str)
11086 return -ENOMEM;
11087 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011088 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -030011089
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011090 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11091 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11092 if (s && !tp->nummodel_str)
11093 return -ENOMEM;
11094
11095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011096}
11097
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011098static int __init probe_for_thinkpad(void)
11099{
11100 int is_thinkpad;
11101
11102 if (acpi_disabled)
11103 return -ENODEV;
11104
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030011105 /* It would be dangerous to run the driver in this case */
11106 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11107 return -ENODEV;
11108
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011109 /*
11110 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -030011111 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011112 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -030011113 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11114 (thinkpad_id.ec_model != 0) ||
11115 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011116
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -030011117 /* The EC handler is required */
11118 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011119 if (!ec_handle) {
11120 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -070011121 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011122 return -ENODEV;
11123 }
11124
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030011125 if (!is_thinkpad && !force_load)
11126 return -ENODEV;
11127
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011128 return 0;
11129}
11130
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011131static void __init thinkpad_acpi_init_banner(void)
11132{
Joe Perches0978e012011-04-04 10:06:25 -070011133 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11134 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011135
Joe Perches0978e012011-04-04 10:06:25 -070011136 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011137 (thinkpad_id.bios_version_str) ?
11138 thinkpad_id.bios_version_str : "unknown",
11139 (thinkpad_id.ec_version_str) ?
11140 thinkpad_id.ec_version_str : "unknown");
11141
11142 BUG_ON(!thinkpad_id.vendor);
11143
11144 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -070011145 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011146 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11147 "IBM" : ((thinkpad_id.vendor ==
11148 PCI_VENDOR_ID_LENOVO) ?
11149 "Lenovo" : "Unknown vendor"),
11150 thinkpad_id.model_str,
11151 (thinkpad_id.nummodel_str) ?
11152 thinkpad_id.nummodel_str : "unknown");
11153}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011154
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011155/* Module init, exit, parameters */
11156
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011157static struct ibm_init_struct ibms_init[] __initdata = {
11158 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011159 .data = &thinkpad_acpi_driver_data,
11160 },
11161 {
11162 .init = hotkey_init,
11163 .data = &hotkey_driver_data,
11164 },
11165 {
11166 .init = bluetooth_init,
11167 .data = &bluetooth_driver_data,
11168 },
11169 {
11170 .init = wan_init,
11171 .data = &wan_driver_data,
11172 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020011173 {
11174 .init = uwb_init,
11175 .data = &uwb_driver_data,
11176 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020011177#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011178 {
11179 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -030011180 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011181 .data = &video_driver_data,
11182 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020011183#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011184 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +010011185 .init = kbdlight_init,
11186 .data = &kbdlight_driver_data,
11187 },
11188 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011189 .init = light_init,
11190 .data = &light_driver_data,
11191 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011192 {
11193 .init = cmos_init,
11194 .data = &cmos_driver_data,
11195 },
11196 {
11197 .init = led_init,
11198 .data = &led_driver_data,
11199 },
11200 {
11201 .init = beep_init,
11202 .data = &beep_driver_data,
11203 },
11204 {
11205 .init = thermal_init,
11206 .data = &thermal_driver_data,
11207 },
11208 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011209 .init = brightness_init,
11210 .data = &brightness_driver_data,
11211 },
11212 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020011213 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011214 .data = &volume_driver_data,
11215 },
11216 {
11217 .init = fan_init,
11218 .data = &fan_driver_data,
11219 },
David Henningsson420f9732013-10-16 23:10:31 +020011220 {
11221 .init = mute_led_init,
11222 .data = &mute_led_driver_data,
11223 },
Ognjen Galic2801b962018-02-07 15:58:44 +010011224 {
11225 .init = tpacpi_battery_init,
11226 .data = &battery_driver_data,
11227 },
Alexander Schremmer110ea1d2019-08-22 13:48:33 +020011228 {
11229 .init = tpacpi_lcdshadow_init,
11230 .data = &lcdshadow_driver_data,
11231 },
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040011232 {
Mark Pearson1ac09652020-11-24 13:11:54 -050011233 .init = tpacpi_proxsensor_init,
11234 .data = &proxsensor_driver_data,
Mark Pearsonacf7f4a2020-07-02 21:23:53 -040011235 },
Mark Pearsonc3bfcd42021-01-11 11:22:37 -050011236 {
11237 .init = tpacpi_dytc_profile_init,
11238 .data = &dytc_profile_driver_data,
11239 },
Nitin Joshid7cbe272021-01-25 11:59:16 +090011240 {
11241 .init = tpacpi_kbdlang_init,
11242 .data = &kbdlang_driver_data,
11243 },
Nitin Joshi3feb52a2021-03-17 11:46:36 +090011244 {
11245 .init = tpacpi_dprc_init,
11246 .data = &dprc_driver_data,
11247 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011248};
11249
Kees Cooke4dca7b2017-10-17 19:04:42 -070011250static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011251{
11252 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011253 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011254
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020011255 if (!kp || !kp->name || !val)
11256 return -EINVAL;
11257
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011258 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11259 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020011260 if (!ibm || !ibm->name)
11261 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011262
11263 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
Andy Shevchenkobe51bd42020-05-11 15:56:24 +030011264 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011265 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011266 strcpy(ibms_init[i].param, val);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011267 return 0;
11268 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011269 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011270
11271 return -EINVAL;
11272}
11273
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011274module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011275MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011276 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011277
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030011278module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011279MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030011280
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011281module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011282MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011283 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030011284
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011285module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011286MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011287 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -030011288
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011289module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011290MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011291 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -030011292
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011293module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011294MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011295 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -020011296
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020011297#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020011298module_param_named(volume_mode, volume_mode, uint, 0444);
11299MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011300 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020011301
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020011302module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11303MODULE_PARM_DESC(volume_capabilities,
Colin Ian Kingd33a7e52019-04-15 13:52:04 +010011304 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020011305
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020011306module_param_named(volume_control, volume_control_allowed, bool, 0444);
11307MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011308 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020011309
Andy Lutomirski9a417ec2014-10-17 17:04:29 -070011310module_param_named(software_mute, software_mute_requested, bool, 0444);
11311MODULE_PARM_DESC(software_mute,
11312 "Request full software mute control");
11313
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020011314/* ALSA module API parameters */
11315module_param_named(index, alsa_index, int, 0444);
11316MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11317module_param_named(id, alsa_id, charp, 0444);
11318MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11319module_param_named(enable, alsa_enable, bool, 0444);
11320MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020011321#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020011322
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +030011323/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011324#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011325 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030011326 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011327
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011328TPACPI_PARAM(hotkey);
11329TPACPI_PARAM(bluetooth);
11330TPACPI_PARAM(video);
11331TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011332TPACPI_PARAM(cmos);
11333TPACPI_PARAM(led);
11334TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011335TPACPI_PARAM(brightness);
11336TPACPI_PARAM(volume);
11337TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030011338
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011339#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011340module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011341MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11342module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11343MODULE_PARM_DESC(wlsw_state,
11344 "Initial state of the emulated WLSW switch");
11345
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011346module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011347MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11348module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11349MODULE_PARM_DESC(bluetooth_state,
11350 "Initial state of the emulated bluetooth switch");
11351
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011352module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011353MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11354module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11355MODULE_PARM_DESC(wwan_state,
11356 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020011357
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000011358module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020011359MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11360module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11361MODULE_PARM_DESC(uwb_state,
11362 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020011363#endif
11364
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011365static void thinkpad_acpi_module_exit(void)
11366{
11367 struct ibm_struct *ibm, *itmp;
11368
11369 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11370
Hans de Goede526ac102021-11-23 22:04:22 +010011371 if (tpacpi_hwmon)
11372 hwmon_device_unregister(tpacpi_hwmon);
Hans de Goede2f5ad082021-11-23 22:04:20 +010011373 if (tp_features.sensors_pdrv_registered)
11374 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11375 if (tp_features.platform_drv_registered)
11376 platform_driver_unregister(&tpacpi_pdriver);
11377
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011378 list_for_each_entry_safe_reverse(ibm, itmp,
11379 &tpacpi_all_drivers,
11380 all_drivers) {
11381 ibm_exit(ibm);
11382 }
11383
11384 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11385
11386 if (tpacpi_inputdev) {
11387 if (tp_features.input_device_registered)
11388 input_unregister_device(tpacpi_inputdev);
11389 else
11390 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +100011391 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011392 }
11393
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011394 if (tpacpi_sensors_pdev)
11395 platform_device_unregister(tpacpi_sensors_pdev);
11396 if (tpacpi_pdev)
11397 platform_device_unregister(tpacpi_pdev);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011398 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011399 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030011400 if (tpacpi_wq)
11401 destroy_workqueue(tpacpi_wq);
11402
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011403 kfree(thinkpad_id.bios_version_str);
11404 kfree(thinkpad_id.ec_version_str);
11405 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +100011406 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020011407}
11408
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020011409
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011410static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011411{
11412 int ret, i;
11413
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -030011414 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11415
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011416 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011417
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011418 ret = get_thinkpad_model_data(&thinkpad_id);
11419 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070011420 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030011421 thinkpad_acpi_module_exit();
11422 return ret;
11423 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011424 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011425 if (ret) {
11426 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030011427 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011429
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011430 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030011431
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030011432 thinkpad_acpi_init_banner();
11433 tpacpi_check_outdated_fw();
11434
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011435 TPACPI_ACPIHANDLE_INIT(ecrd);
11436 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011437
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030011438 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11439 if (!tpacpi_wq) {
11440 thinkpad_acpi_module_exit();
11441 return -ENOMEM;
11442 }
11443
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011444 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011445 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -070011446 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011447 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070011448 return -ENODEV;
11449 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -040011450
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011451 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011452 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011453 NULL, 0);
11454 if (IS_ERR(tpacpi_pdev)) {
11455 ret = PTR_ERR(tpacpi_pdev);
11456 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070011457 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030011458 thinkpad_acpi_module_exit();
11459 return ret;
11460 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011461 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020011462 TPACPI_HWMON_DRVR_NAME,
11463 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011464 if (IS_ERR(tpacpi_sensors_pdev)) {
11465 ret = PTR_ERR(tpacpi_sensors_pdev);
11466 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070011467 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030011468 thinkpad_acpi_module_exit();
11469 return ret;
11470 }
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070011471
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -030011472 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011473 tpacpi_inputdev = input_allocate_device();
11474 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011475 thinkpad_acpi_module_exit();
11476 return -ENOMEM;
11477 } else {
11478 /* Prepare input device, but don't register */
11479 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011480 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011481 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030011482 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011483 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11484 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +000011485 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011486 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -030011487
11488 /* Init subdriver dependencies */
11489 tpacpi_detect_brightness_capabilities();
11490
11491 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030011492 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11493 ret = ibm_init(&ibms_init[i]);
11494 if (ret >= 0 && *ibms_init[i].param)
11495 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011496 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011497 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070011498 return ret;
11499 }
11500 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -030011501
11502 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11503
Hans de Goede2f5ad082021-11-23 22:04:20 +010011504 ret = platform_driver_register(&tpacpi_pdriver);
11505 if (ret) {
11506 pr_err("unable to register main platform driver\n");
11507 thinkpad_acpi_module_exit();
11508 return ret;
11509 }
11510 tp_features.platform_drv_registered = 1;
11511
11512 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11513 if (ret) {
11514 pr_err("unable to register hwmon platform driver\n");
11515 thinkpad_acpi_module_exit();
11516 return ret;
11517 }
11518 tp_features.sensors_pdrv_registered = 1;
11519
Hans de Goede526ac102021-11-23 22:04:22 +010011520 tpacpi_hwmon = hwmon_device_register_with_groups(
11521 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
11522 if (IS_ERR(tpacpi_hwmon)) {
11523 ret = PTR_ERR(tpacpi_hwmon);
11524 tpacpi_hwmon = NULL;
11525 pr_err("unable to register hwmon device\n");
11526 thinkpad_acpi_module_exit();
11527 return ret;
11528 }
11529
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011530 ret = input_register_device(tpacpi_inputdev);
11531 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -070011532 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030011533 thinkpad_acpi_module_exit();
11534 return ret;
11535 } else {
11536 tp_features.input_device_registered = 1;
11537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538
11539 return 0;
11540}
11541
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -030011542MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11543
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011544/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000011545 * This will autoload the driver in almost every ThinkPad
11546 * in widespread use.
11547 *
11548 * Only _VERY_ old models, like the 240, 240x and 570 lack
11549 * the HKEY event interface.
11550 */
11551MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
11552
11553/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011554 * DMI matching for module autoloading
11555 *
Alexander A. Klimov19206a12020-07-13 21:06:28 +020011556 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11557 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011558 *
11559 * Only models listed in thinkwiki will be supported, so add yours
11560 * if it is not there yet.
11561 */
11562#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +010011563 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011564
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011565/* Ancient thinkpad BIOSes have to be identified by
11566 * BIOS type or model number, and there are far less
11567 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000011568IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011569
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +000011570MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
11571MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020011572MODULE_DESCRIPTION(TPACPI_DESC);
11573MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020011574MODULE_LICENSE("GPL");
11575
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030011576module_init(thinkpad_acpi_module_init);
11577module_exit(thinkpad_acpi_module_exit);